0
我閱讀文檔如何與視圖模型進行通信,然後嘗試使用它。但是不能改變。就像從GitHub上的例子,我創建CommunicationViewModel:兩個視圖模型如何使用catel進行通信wpf
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
}
}
而且MainWindowViewModel
[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
的的OpenView
<ListView Name="DatabasesListView" MinHeight="150" Width="200" ItemsSource="{Binding Databases}" SelectedItem="{Binding Property}" />
的MainWindowView
<Label Content="{Binding Property}" HorizontalAlignment="Center" Margin="0 0 0 0" FontSize="14" />
如何溝通這兩個視圖模型。在例子中工作在我的情況下不工作。當我更換Property
時,每次都是null
。是不是每次都給我一些例外是空的。
編輯:我用了兩個數據窗口,不使用用戶控件
我問,等待一些答案 – evelikov92