Able to return a string from the async method
Fixed ObservableObject
This commit is contained in:
@@ -16,7 +16,10 @@ namespace WpfViewModelFirst
|
|||||||
protected override void OnStartup(StartupEventArgs e)
|
protected override void OnStartup(StartupEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnStartup(e);
|
base.OnStartup(e);
|
||||||
MainWindow mainWindow = new() { DataContext = new MainWindowViewModel() };
|
MainWindow mainWindow = new()
|
||||||
|
{
|
||||||
|
DataContext = new MainWindowViewModel()
|
||||||
|
};
|
||||||
mainWindow.Show();
|
mainWindow.Show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,31 @@
|
|||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:local="clr-namespace:WpfViewModelFirst"
|
xmlns:local="clr-namespace:WpfViewModelFirst"
|
||||||
|
xmlns:part1="clr-namespace:WpfViewModelFirst.Part1"
|
||||||
|
xmlns:part2="clr-namespace:WpfViewModelFirst.Part2"
|
||||||
mc:Ignorable="d"
|
mc:Ignorable="d"
|
||||||
|
d:DataContext="{d:DesignInstance Type={x:Type local:MainWindowViewModel}}"
|
||||||
Title="MainWindow" Height="450" Width="800">
|
Title="MainWindow" Height="450" Width="800">
|
||||||
<Grid>
|
<Grid Background="DarkRed">
|
||||||
|
<Grid.RowDefinitions>
|
||||||
|
<RowDefinition/>
|
||||||
|
<RowDefinition Height="auto"/>
|
||||||
|
</Grid.RowDefinitions>
|
||||||
|
<ContentControl Grid.Row="0"
|
||||||
|
Visibility="Visible"
|
||||||
|
Content="{Binding CurrentViewModel}">
|
||||||
|
<ContentControl.Resources>
|
||||||
|
<DataTemplate DataType="{x:Type part1:Part1ViewModel}">
|
||||||
|
<part1:Part1View/>
|
||||||
|
</DataTemplate>
|
||||||
|
<DataTemplate DataType="{x:Type part2:Part2ViewModel}">
|
||||||
|
<part2:Part2View/>
|
||||||
|
</DataTemplate>
|
||||||
|
</ContentControl.Resources>
|
||||||
|
</ContentControl>
|
||||||
|
<Button Grid.Row="1"
|
||||||
|
HorizontalAlignment="Center"
|
||||||
|
Command="{Binding StartCommand}"
|
||||||
|
Content="Start"/>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Window>
|
</Window>
|
||||||
|
|||||||
@@ -3,11 +3,45 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows.Input;
|
||||||
|
using System.Windows.Threading;
|
||||||
|
using WpfViewModelFirst.Part1;
|
||||||
|
using WpfViewModelFirst.Part2;
|
||||||
|
|
||||||
namespace WpfViewModelFirst
|
namespace WpfViewModelFirst
|
||||||
{
|
{
|
||||||
public class MainWindowViewModel : ViewModelBase
|
public class MainWindowViewModel : ViewModelBase
|
||||||
{
|
{
|
||||||
|
public MainWindowViewModel()
|
||||||
|
{
|
||||||
|
StartCommand = new CustomCommand(Part1);
|
||||||
|
}
|
||||||
|
public ICommand StartCommand { get; set; }
|
||||||
|
|
||||||
|
public void Part1(object? e)
|
||||||
|
{
|
||||||
|
CurrentViewModel = null;
|
||||||
|
CurrentViewModel = new Part1ViewModel(Part1Action);
|
||||||
|
// MainFrameViewModel = new Part2ViewModel();
|
||||||
|
}
|
||||||
|
private void Part1Action()
|
||||||
|
{
|
||||||
|
CurrentViewModel = null;
|
||||||
|
Task.Run(async () => await LongDelay()).ContinueWith(c =>
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine(c.Result);
|
||||||
|
CurrentViewModel = new Part2ViewModel();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<string> LongDelay()
|
||||||
|
{
|
||||||
|
await Task.Delay(500);
|
||||||
|
return "Completed";
|
||||||
|
}
|
||||||
|
|
||||||
|
private ViewModelBase? _mainFrameViewModel;
|
||||||
|
public ViewModelBase? CurrentViewModel { get => _mainFrameViewModel; set => SetProperty(ref _mainFrameViewModel, value); }
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -8,7 +9,7 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace WpfViewModelFirst
|
namespace WpfViewModelFirst
|
||||||
{
|
{
|
||||||
public class ObservableObject
|
public class ObservableObject : INotifyPropertyChanged
|
||||||
{
|
{
|
||||||
public event PropertyChangedEventHandler? PropertyChanged;
|
public event PropertyChangedEventHandler? PropertyChanged;
|
||||||
protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = "")
|
protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = "")
|
||||||
@@ -16,7 +17,7 @@ namespace WpfViewModelFirst
|
|||||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual bool Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = "")
|
protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = "")
|
||||||
{
|
{
|
||||||
if (EqualityComparer<T>.Default.Equals(storage, value))
|
if (EqualityComparer<T>.Default.Equals(storage, value))
|
||||||
{
|
{
|
||||||
@@ -26,6 +27,5 @@ namespace WpfViewModelFirst
|
|||||||
this.RaisePropertyChanged(propertyName);
|
this.RaisePropertyChanged(propertyName);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
18
WpfViewModelFirst/Part1/Part1View.xaml
Normal file
18
WpfViewModelFirst/Part1/Part1View.xaml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<UserControl x:Class="WpfViewModelFirst.Part1.Part1View"
|
||||||
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
|
xmlns:local="clr-namespace:WpfViewModelFirst.Part1"
|
||||||
|
mc:Ignorable="d"
|
||||||
|
d:DataContext="{d:DesignInstance Type={x:Type local:Part1ViewModel}}"
|
||||||
|
d:DesignHeight="450" d:DesignWidth="800">
|
||||||
|
<Grid>
|
||||||
|
<Grid.RowDefinitions>
|
||||||
|
<RowDefinition/>
|
||||||
|
</Grid.RowDefinitions>
|
||||||
|
<Button Grid.Row="0"
|
||||||
|
Content="Finish"
|
||||||
|
Command="{Binding FinishCommand}"/>
|
||||||
|
</Grid>
|
||||||
|
</UserControl>
|
||||||
28
WpfViewModelFirst/Part1/Part1View.xaml.cs
Normal file
28
WpfViewModelFirst/Part1/Part1View.xaml.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows;
|
||||||
|
using System.Windows.Controls;
|
||||||
|
using System.Windows.Data;
|
||||||
|
using System.Windows.Documents;
|
||||||
|
using System.Windows.Input;
|
||||||
|
using System.Windows.Media;
|
||||||
|
using System.Windows.Media.Imaging;
|
||||||
|
using System.Windows.Navigation;
|
||||||
|
using System.Windows.Shapes;
|
||||||
|
|
||||||
|
namespace WpfViewModelFirst.Part1
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Interaction logic for Part1View.xaml
|
||||||
|
/// </summary>
|
||||||
|
public partial class Part1View : UserControl
|
||||||
|
{
|
||||||
|
public Part1View()
|
||||||
|
{
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
26
WpfViewModelFirst/Part1/Part1ViewModel.cs
Normal file
26
WpfViewModelFirst/Part1/Part1ViewModel.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows.Input;
|
||||||
|
|
||||||
|
namespace WpfViewModelFirst.Part1
|
||||||
|
{
|
||||||
|
public class Part1ViewModel : ViewModelBase
|
||||||
|
{
|
||||||
|
private readonly Action _finishAction;
|
||||||
|
|
||||||
|
public Part1ViewModel(Action finishAction)
|
||||||
|
{
|
||||||
|
_finishAction = finishAction;
|
||||||
|
FinishCommand = new CustomCommand(Finish);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ICommand FinishCommand { get; set; }
|
||||||
|
private void Finish(object? e)
|
||||||
|
{
|
||||||
|
_finishAction.Invoke();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
WpfViewModelFirst/Part2/Part2View.xaml
Normal file
13
WpfViewModelFirst/Part2/Part2View.xaml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<UserControl x:Class="WpfViewModelFirst.Part2.Part2View"
|
||||||
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
|
xmlns:local="clr-namespace:WpfViewModelFirst.Part2"
|
||||||
|
mc:Ignorable="d"
|
||||||
|
d:DataContext="{d:DesignInstance Type={x:Type local:Part2ViewModel}}"
|
||||||
|
d:DesignHeight="450" d:DesignWidth="800">
|
||||||
|
<Grid>
|
||||||
|
<TextBlock Text="Part 2"/>
|
||||||
|
</Grid>
|
||||||
|
</UserControl>
|
||||||
28
WpfViewModelFirst/Part2/Part2View.xaml.cs
Normal file
28
WpfViewModelFirst/Part2/Part2View.xaml.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows;
|
||||||
|
using System.Windows.Controls;
|
||||||
|
using System.Windows.Data;
|
||||||
|
using System.Windows.Documents;
|
||||||
|
using System.Windows.Input;
|
||||||
|
using System.Windows.Media;
|
||||||
|
using System.Windows.Media.Imaging;
|
||||||
|
using System.Windows.Navigation;
|
||||||
|
using System.Windows.Shapes;
|
||||||
|
|
||||||
|
namespace WpfViewModelFirst.Part2
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Interaction logic for Part2View.xaml
|
||||||
|
/// </summary>
|
||||||
|
public partial class Part2View : UserControl
|
||||||
|
{
|
||||||
|
public Part2View()
|
||||||
|
{
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
WpfViewModelFirst/Part2/Part2ViewModel.cs
Normal file
13
WpfViewModelFirst/Part2/Part2ViewModel.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace WpfViewModelFirst.Part2
|
||||||
|
{
|
||||||
|
public class Part2ViewModel : ViewModelBase
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user