我有一个MVVM WPF应用程序,在完成一些长任务时,我会从中显示一个启动屏幕。
它的代码背后只有一个构造函数,在该构造函数中只执行InitializeComponent和加载的事件窗口。
在我的主MVVM WPF应用程序中,当我执行一个长任务时,我实例化这个窗口并显示初始屏幕。
Window mySplash = new SplashScreen("Loading or whatever I want");
由于这个初始屏幕非常简单,只是一个初始屏幕,我认为在这里应用MVVM没有任何意义,因此在后面的代码中,我创建了一个私有属性,我使用作为参数传递给构造函数的字符串设置该属性。然后,我用这个私有属性绑定视图中的标签,最后在代码隐藏(view)中实现INotifyPropertyChanged。这里没有模型,模型视图。
这是正确的方法吗?或者还有别的办法吗?
x:FieldModifier="public"
然后访问它一旦我实例化启动屏幕,但我不喜欢这个解决方案,我不想暴露标签外面。
根据主MVVM WPF应用程序中的视图模型,我执行以下操作:
Window splashScreen = new SplashScreen("Loading ...");
启动屏幕窗口:
<Window x:Class="My.Apps.WPF.SplashScreen"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Label Grid.Row="0" Content="{Binding Path=Message}"/>
</Grid>
</Window>
闪屏代码:
public partial class SplashScreen: Window
{
public string Message
{
get
{
return (string)GetValue(MessageProperty);
}
set { SetValue(MessageProperty, value); }
}
public static readonly DependencyProperty
MessageProperty =
DependencyProperty.Register("Message",
typeof(string), typeof(System.Window.Controls.Label),
new UIPropertyMetadata("Working, wait ..."));
public SplashScreen(string message)
{
InitializeComponent();
if (!String.IsNullOrEmpty(message))
this.Message = message;
}
}
我已经为标签设置了默认值。如果未将其作为参数传递给构造函数,则将使用它。
它不起作用,在xaml预览中,未在Visual Studio IDE环境的标签中显示默认消息。同样由于某些原因,当我将来自视图模型的自定义消息作为参数传递时,它不会显示在标签中。我做错了什么?