RelayCommand

master
Yik Teng Hie 3 years ago
parent c69e770151
commit de981433d2

@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection;
using MvvmCommunityApp.interfaces; using MvvmCommunityApp.interfaces;
using MvvmCommunityApp.services; using MvvmCommunityApp.services;
using MvvmCommunityApp.viewmodels; using MvvmCommunityApp.viewmodels;
using System;
using System.Windows; using System.Windows;
namespace MvvmCommunityApp namespace MvvmCommunityApp
@ -12,17 +13,35 @@ namespace MvvmCommunityApp
/// </summary> /// </summary>
public partial class App : Application public partial class App : Application
{ {
protected override void OnStartup(StartupEventArgs e) public new static App Current => (App)Application.Current;
public App()
{ {
base.OnStartup(e); // !!! Add constructor initialization HERE !!!!
// configure mvvm ioc
ConfigureServices();
}
private void ConfigureServices() =>
// setup mvvm ioc // setup mvvm ioc
Ioc.Default.ConfigureServices( Ioc.Default.ConfigureServices(
new ServiceCollection() new ServiceCollection()
.AddTransient<IocPageViewModel>() .AddTransient<IocPageViewModel>()
.AddSingleton<IDemoService, DemoService>() .AddSingleton<IDemoService, DemoService>()
.AddSingleton<MainWindowViewModel>()
.BuildServiceProvider()); .BuildServiceProvider());
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// setup mvvm ioc
//Ioc.Default.ConfigureServices(
// new ServiceCollection()
// .AddTransient<IocPageViewModel>()
// .AddSingleton<IDemoService, DemoService>()
// .BuildServiceProvider());
// load the main / entry page // load the main / entry page
MainWindow wnd = new MainWindow(); MainWindow wnd = new MainWindow();

@ -8,7 +8,9 @@
Title="MainWindow" Height="450" Width="800"> Title="MainWindow" Height="450" Width="800">
<Grid> <Grid>
<StackPanel> <StackPanel>
<TextBlock Text="{Binding MyName}" /> <TextBlock Text="{Binding Title}" />
<Button Content="Add"
Command="{Binding AddCommand}" />
</StackPanel> </StackPanel>
</Grid> </Grid>
</Window> </Window>

@ -19,7 +19,7 @@ namespace MvvmCommunityApp
InitializeComponent(); InitializeComponent();
DataContext = Ioc.Default.GetRequiredService<IocPageViewModel>(); DataContext = Ioc.Default.GetRequiredService<MainWindowViewModel>();
} }
} }
} }

@ -0,0 +1,28 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MvvmCommunityApp.viewmodels
{
public class MainWindowViewModel : ObservableObject
{
public string Title { get; set; }
public RelayCommand AddCommand { get; }
public MainWindowViewModel()
{
Title = "MyIocName";
//
AddCommand = new RelayCommand(DoAdd);
}
private void DoAdd()
{
Console.WriteLine("Do Add");
}
}
}
Loading…
Cancel
Save