1 建议 消息的Token存放在一个公共区,这样send和register不要为取名字发愁,还是强类型的
public class MessageTokens { public static readonly string ReturnTeacher = Guid.NewGuid().ToString(); public static readonly string ReturnTeacher2 = Guid.NewGuid().ToString(); }
关于PropertyChangedMessage
新增一个
public partial class LoginUserModel : ObservableRecipient { [ObservableProperty] [AlsoBroadcastChange] private string username; [ObservableProperty] [AlsoBroadcastChange] private string password; public LoginUserModel() { WeakReferenceMessenger.Default.Register>(this, (r, msgs) => { if (msgs.PropertyName == nameof(Username)) { } else if (msgs.PropertyName == nameof(Password)) { } }); } }
使用 [AlsoBroadcastChange]特性时候,当前类必须继承ObservableRecipient
然后注册一个PropertyChangedMessage的消息,当某个属性调用了 属性通知方法,就会自动到PropertyChangedMessage这个消息中处理,你可以获得新值,旧值,属性名。
那么我们在TwoWindow上新增两个文本框,绑定这个model
public partial class TwoWindowViewModel : ObservableValidator { [ObservableProperty] private LoginUserModel loginuser=new LoginUserModel();
然后xaml
运行项目,打开TwoWindow,打上断电,输入内容时候,进入了断点如下
这是我第二次进入断点,第一次输入了A,然后第二次输入Y
它公开了一个 Broadcast
Broadcast的老式用法,下面是源码生成器生成的
我直接拷贝官方文档的东西了,可以对照着我这个系列第一天博客思想参考
public sealed partial class App : Application { public App() { Services = ConfigureServices(); this.InitializeComponent(); } ////// Gets the current public new static App Current => (App)Application.Current; ///instance in use /// /// Gets the public IServiceProvider Services { get; } ///instance to resolve application services. /// /// Configures the services for the application. /// private static IServiceProvider ConfigureServices() { var services = new ServiceCollection(); services.AddSingleton(); services.AddSingleton (); services.AddSingleton (); services.AddSingleton (); services.AddSingleton (); return services.BuildServiceProvider(); } }
其他地方使用
IFilesService filesService = App.Current.Services.GetService();
构造函数注入
public class FileLogger : IFileLogger { private readonly IFilesService FileService; private readonly IConsoleService ConsoleService; public FileLogger( IFilesService fileService, IConsoleService consoleService) { FileService = fileService; ConsoleService = consoleService; } // Methods for the IFileLogger interface here... }
增加一个新的接口和实现,需要在统一的地方注册
////// Configures the services for the application. /// private static IServiceProvider ConfigureServices() { var services = new ServiceCollection(); services.AddSingleton(); services.AddSingleton (); services.AddSingleton (); return services.BuildServiceProvider(); } // Retrieve a logger service with constructor injection IFileLogger fileLogger = App.Current.Services.GetService ();
关于viewmodel的实例管理
////// Configures the services for the application. /// private static IServiceProvider ConfigureServices() { var services = new ServiceCollection(); // Services services.AddSingleton(); services.AddSingleton (); // Viewmodels services.AddTransient (); return services.BuildServiceProvider(); }
vm都是瞬时的,用完就销毁,服务是单例
页面的话,在xaml.cs下指定vm
public ContactsView() { this.InitializeComponent(); this.DataContext = App.Current.Services.GetService(); }
我们那个直接xaml上指定。
官方文档下方也加了 推荐使用 我第一天使用的那个DI
Dependency injection in ASP.NET Core | Microsoft Docs
services.AddTransient
();//瞬时模式(每次获取对象的时候,容器都会重新给我们new一个新的对象返回) services.AddSingleton
();//单例模式(第一次请求的时候,创建对象,以后再次请求都是同一个对象) services.AddScoped
(); //作用域模式(在一个请求中,多次使用,这时候提供的是同一个对象)