Tag: mvvm light

使用本地/匿名操作时不会触发MvvmLight消息

当我使用匿名操作时,MvvmLight messenger有时不起作用。 如果我传递成员变量或方法作为操作它工作正常,但使用匿名lambda或局部变量不起作用。 private SongCollection songCollection; Action c; public MyService(SongCollection songCollection) { this.songCollection = songCollection; Action a = (bool isLoading) => { ChangeSong(songCollection.GetFirstSong()); }; Action b = OnLoadingComplete; //Using this instead of ‘a’ works. //c = a; //Uncommenting this line makes it work, even if using ‘a’. Messenger.Default.Register(this, “IsLoading”, a); //Doesn’t work. } 我猜它与垃圾收集或MvvmLight在幕后工作的方式有关。 还是我错过了一些明显的东西? […]

如何在创建新视图时初始化视图模型中的属性?

我有一个应用程序打开一个允许您搜索数据的视图。 但是,为了进行搜索,用户必须选择他想要搜索的类别。 目前,我正在试图弄清楚如何将所选类别从主视图模型(作为int)传递到新搜索视图的视图模型。 目前我正在尝试在主视图中使用这样的东西: 假设我有两个视图View1和View2各自的视图模型。 View2ViewlModel看起来像这样: public class View2ViewlModel : ViewModelBase { private IDataService _dataService; public int DivisionIdnt {get; set;} public View2ViewModel(IDataService dataService) { _dataService = dataService; } } 在View1我们在收到消息时创建并打开View2 。 public View2() { InitializeComponent(); Messenger.Default.Register<NotificationMessage>(this, (m) => NotificationMesageReceived(m, m.Content)); } private void NotificationMesageReceived(NotificationMessage msg, int divisionIdnt) { if (msg.Notification == “SearchCred”) { var findCredentialView […]

当我想要双击时如何避免点击?

我在WPF中有一个应用程序和一个按钮。 在这个buttun中,我想要实现代码的click事件,但我希望当用鼠标双击时,执行其他代码但不执行click事件的代码。 问题是click事件的代码总是被执行,我不知道当我做doulbe点击时是否有办法避免执行click事件。 我遵循MVVM模式,并使用MVVM灯将事件转换为命令。 谢谢。

在WPF应用程序中使用MVVM / MVVMLight时如何与UI元素交互

根据我下面的代码,我希望能够在单击Button 1时更改Button 2的背景颜色。 XAML文件 ViewModel文件 public class MyViewModel : ViewModelBase { public ICommand Button1Command{get;private set;} public MyViewModel(){ Button1Command = new RelayCommand(() => button1_Click()); } private void button1_Click() { Console.WriteLine(“Button 1 clicked”); // how can I change the background color of Button 2 here this.Dispatcher.Invoke(() => { Button2.Background = Brushes.Red; }); } }

如何在SimpleIOC中注册包含参数的类实例

我需要创建一个ViewModel实例,并在创建时将特定参数传递给ViewModel。 同时,此ViewModel实例应在SimpleIOC中注册 我认为这是它的方法: SimpleIoc.Register Method (Func, String, Boolean) 对于即时创建的最后一个参数,设置为true 。 因此,如果我理解正确,此方法需要引用将创建我的ViewModel实例的方法。 这看起来像一个ClassFactory。 我试着自己做,但我得到的只是 cannot convert from to System.Func 所以它似乎总是传递类的实例,而不是应该创建它的方法。 有人能给出一个简短的例子,说明我是如何让这个工作的 public class ClassFactory { public ChatWindow CreateChatWindow(RosterItemX ri) { return new ChatWindow(ri); } } public class ViewModelLocator { . . . . public static void CreateWindow(RosterItemX riv) { ClassFactory cf = new ClassFactory; SimpleIoc.Default.Register(cf.CreateChatWindow(ri), “key”, […]

对于ViewModelLocator,“无法确定调用者的应用程序标识”

在app.xaml中,我在定义ViewModelLocator时出错。 应用程序启动并运行良好,但它在Studio中让我很烦。 想知道,这可能是一个原因。 突出显示的行是 错误: 错误1无法确定调用方的应用程序标识。 如果要进入vm:ViewModelLocator,那里没有任何错误迹象。 我应该重新检查我的所有视图模型并添加IsoStorage设计时保护吗? if (!System.ComponentModel.DesignerProperties.IsInDesignTool) { settings = IsolatedStorageSettings.ApplicationSettings; }

MVVM WPF更改窗口并关闭之前的

作为MVVM的所有架构的首发,我对一个窗口到另一个窗口之间的导航有一些疑问。 我正在使用Framework MVVM Light。 我期望的行为在WinForms中是这样的: GeneralWindow gw = new GeneralWindow(); this.Hide(); //或关闭gw.Show(); 我已经失去了几个小时试图使用信使找到一些提示,但我发现我必须在视图中使用代码隐藏的方法,这不是很MVVMish。 最好的问候,并提前感谢您。

绑定到另一个视图模型

我试图将可见性属性绑定到我在viewmodel( MainViewModel )中创建的函数,但我收到此错误: mscorlib.dll System.Windows.Data中出现类型’System.IO.FileNotFoundException’的第一次机会exception错误:BindingExpression路径错误:’Locator”System.String’上找不到’Main’属性(HashCode = -191326816) 。 BindingExpression:Path =’Main.TilesHomeViewVisible’DataItem =’Locator’(HashCode = -191326816); target元素是’myApp.Views.TilesHomeView’(Name =’myTilesHomeView’); target属性是’Visibility’(类型’System.Windows.Visibility’).. 根据我从错误中理解,它正在TilesHomeViewVisible寻找TilesHomeViewVisible函数,而它实际上是在MainViewModel 。 在绑定表达式中,我如何定位MainViewModel呢? 编辑:我有一个’ViewModelLocator’集成。 这是我的ViewModelLocator: … public ViewModelLocator() { ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); } public MainViewModel Main { get { return ServiceLocator.Current.GetInstance(); } } public TilesHomeViewModel TilesVM { get { return ServiceLocator.Current.GetInstance(); } } … 我的App.xaml: 在我的MainPage.xaml和链接到定位器的地方,我有: … […]

WPF和MVVM Light – 通过Messenger关闭特定的子窗口

在我的项目中,我可以打开多个子窗口,显示和返回信息,然后单击按钮关闭它们。 我遇到的问题是单击“接受”或“取消”按钮将关闭所有打开的窗口。 我需要找到一种只关闭正确窗口的方法,但我无法弄清楚如何。 我正在使用MVVM Light,我认为令牌可能是关键,但我还没弄清楚如何使它们工作。 如果有人能帮助我,我会非常感激。 在MainWindow中创建子窗口: Messenger.Default.Register(this, message => { var uniqueKey = System.Guid.NewGuid().ToString(); var adventurerWindowVM = SimpleIoc.Default.GetInstance(uniqueKey); adventurerWindowVM.Adv = message.Argument; var adventurerWindow = new AdventurerView() { DataContext = adventurerWindowVM, Owner = this }; adventurerWindow.Closed += (sender, args) => SimpleIoc.Default.Unregister(uniqueKey); adventurerWindow.Show(); }); 从AdventurerViewModel发送关闭窗口消息: private void ExecuteAcceptCommand() { Messenger.Default.Send(Adv.Name); Messenger.Default.Send(new CloseWindowMessage()); } 在AdventurerView中收到关闭窗口的消息: Messenger.Default.Register(this, x […]

WPF MVVM Light – 在viewmodel中取消属性更改 – 即使值与以前相同,也是RaisePropertyChanged

我有一个绑定到combobox的属性(在viewmodel中)。 当viewmodel属性更改时,它使用Messenger来告诉另一个视图模型。 然后,这个其他视图模型决定是否可以,如果不是,我想取消并将旧值发送回视图。 我想我可以先将值设置为新值,然后再将其设置为此值。 但是有更优雅的疗法吗? 失败的代码 public DeckType SelectedDeckType { get { return _selectedDeckType; } set { DeckTypeMessage deckTypeMessage = new DeckTypeMessage(value); Messenger.Default.Send(deckTypeMessage); if (deckTypeMessage.IsCancel) { //Some background magic finds out the value of this property is still the same? //So the combobox does not revert! //I can hack this but is there some way […]