两个视图模型如何使用卡特尔 WPF 进行通信
本文关键字:卡特尔 何使用 WPF 通信 模型 两个 视图 | 更新日期: 2023-09-27 18:34:02
我阅读了如何与视图模型通信的文档,并尝试了一下。但无法改变。就像github中的例子一样,我创建了CommunicationViewModel:
public abstract class CommunicationViewModel : ViewModelBase
{
public CommunicationViewModel()
{
ExampleCommand = new Command(OnExampleCommandExecute);
}
public string Property
{
get { return GetValue<string>(PropertyProperty); }
set { SetValue(PropertyProperty, value); }
}
public static readonly PropertyData PropertyProperty = RegisterProperty("Property", typeof(string), null);
public void UpdateProperty(string modifier)
{
Property = modifier.ToString();
}
public Command ExampleCommand { get; private set; }
private void OnExampleCommandExecute()
{
ExecuteCommand();
}
protected abstract void ExecuteCommand();
protected void AddPropertyChange(string propertyName, Type senderType)
{
Argument.IsNotNull(() => propertyName);
Argument.IsNotNull(() => senderType);
// do some stuff
}
}
然后,我想从OpenViewModel将信息发送到MainWindowViewModel。
OpenViewModel 源代码:
public class OpenViewModel : CommunicationViewModel
{
private readonly IMessageService iMessage;
private readonly IPleaseWaitService iPleaseWait;
private readonly ILanguageService iLanguage;
public OpenViewModel(IMessageService iMessageService, IPleaseWaitService iPleaseWaitService, ILanguageService iLanguageService)
{
Argument.IsNotNull(() => iMessageService);
Argument.IsNotNull(() => iPleaseWaitService);
Argument.IsNotNull(() => iLanguageService);
iMessage = iMessageService;
iPleaseWait = iPleaseWaitService;
iLanguage = iLanguageService;
ChooseDatabaseForOpen = new Command(HandleChooseDatabaseForOpen);
}
public ObservableCollection<string> Databases
{
get { return new ObservableCollection<string>(SettingGenerator.ReadFewSameDatabaseSettings("Databases", "Database", "Value")); }
set { SetValue(DatabasesProperty, value); }
}
public static readonly PropertyData DatabasesProperty = RegisterProperty("Databases", typeof(ObservableCollection<string>), () => new ObservableCollection<string>());
public Command ChooseDatabaseForOpen { get; private set; }
protected override async Task InitializeAsync()
{
await base.InitializeAsync();
// TODO: subscribe to events here
}
protected override async Task CloseAsync()
{
// TODO: unsubscribe from events here
await base.CloseAsync();
}
private void HandleChooseDatabaseForOpen()
{
iPleaseWait.Show(iLanguage.GetString("PleaseWaitMessage"));
SettingGenerator.ChangeDatabaseSetting(null, "ActiveDatabase", this.Database);
iPleaseWait.Hide();
}
protected override void ExecuteCommand()
{
// InterestedIn does not required any custom logic
}
}
和主窗口视图模型
[InterestedIn(typeof(OpenViewModel))]
public class MainWindowViewModel : CommunicationViewModel
{
private const string PATH_TO_CHECKED_IMAGE = @"./../Resources/Images/Icons/Settings/Checked Checkbox.png";
private const string PATH_TO_UNCHECKED_IMAGE = @"./../Resources/Images/Icons/Settings/Checkbox.png";
private readonly IUIVisualizerService iUIVisualizer;
private readonly IPleaseWaitService iPleaseWait;
private readonly IMessageService iMessage;
private readonly IOpenFileService iOpenFile;
private readonly ILanguageService iLanguage;
private readonly ISplashScreenService iSplashScreen;
private readonly string settingFile = AppDomain.CurrentDomain.BaseDirectory + FileConstants.PATH_TO_SETTINGS_FILE;
public MainWindowViewModel(IUIVisualizerService iUIVisualizerService, IPleaseWaitService iPleaseWaitService, IMessageService iMessageService, IOpenFileService iOpenFileService, ILanguageService iLanguageService, ISplashScreenService iSplashScreenService)
{
Argument.IsNotNull(() => iOpenFileService);
Argument.IsNotNull(() => iUIVisualizerService);
Argument.IsNotNull(() => iMessageService);
Argument.IsNotNull(() => iPleaseWaitService);
Argument.IsNotNull(() => iLanguageService);
Argument.IsNotNull(() => iSplashScreenService);
this.iOpenFile = iOpenFileService;
this.iUIVisualizer = iUIVisualizerService;
this.iMessage = iMessageService;
this.iPleaseWait = iPleaseWaitService;
this.iLanguage = iLanguageService;
this.iSplashScreen = iSplashScreenService;
Minimize = new Command(HandleMinimizeCommand);
Restore = new Command(HandleRestoreCommand);
CloseApplication = new Command(HandleCloseApplicationCommand);
if (SettingGenerator.ReadDatabaseSetting(null, "Update") == "Automatic")
{
this.AutoUpgradeCheckbox = PATH_TO_CHECKED_IMAGE;
}
else if (SettingGenerator.ReadDatabaseSetting(null, "Update") == "Manual")
{
this.AutoUpgradeCheckbox = PATH_TO_UNCHECKED_IMAGE;
}
SelectIconForMainMenuItems();
//this.Property = this.Database;
}
protected override void OnViewModelPropertyChanged(IViewModel viewModel, string propertyName)
{
AddPropertyChange(propertyName, viewModel.GetType());
}
protected override void ExecuteCommand()
{
// InterestedIn does not required any custom logic
}
}
从 xaml
开放视图
<ListView Name="DatabasesListView" MinHeight="150" Width="200" ItemsSource="{Binding Databases}" SelectedItem="{Binding Property}" />
主窗口视图
<Label Content="{Binding Property}" HorizontalAlignment="Center" Margin="0 0 0 0" FontSize="14" />
如何传达这两个视图模型。例如,在我的情况下工作不起作用。当我每次都改变Property
是null
.不是每次都给我一些例外是空的。
编辑:我使用了两个数据窗口,而不是使用用户控件
如文档所述,视图模型之间有几种通信方法:
- 服务业
- 感兴趣(您正在使用的内容)
- 消息调解器
看起来你正在做的事情是正确的。如果您觉得它没有按预期工作,请在官方跟踪器中提供重现。