【发布时间】:2022-10-18 05:36:14
【问题描述】:
我有一个ShellView 和ShellViewModel。在该 ShellView 中,我有一个页面调用 FirstPage,我已将其嵌入为框架,在 Startup 上打开它是没有问题的。到目前为止,这就是我所做的。
ShellView.xaml.cs
public partial class ShellView : Window
{
public ShellView()
{
InitializeComponent();
FirstPage.Content = new FirstPage();
}
}
ShellView.xaml
<Window x:Class="CaliburnMicroDemo.Views.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CaliburnMicroDemo.Views"
mc:Ignorable="d"
Title="ShellWindow" Height="450" Width="800">
<Grid>
<Frame x:Name="FirstPage" Source="FirstPage.xaml" NavigationUIVisibility="Hidden"/>
</Grid>
</Window>
第一页.xaml.cs
public partial class FirstPage : Page
{
public FirstPage()
{
InitializeComponent();
this.DataContext = new FirstPageViewModel();
}
}
第一页.xaml
<Page x:Class="CaliburnMicroDemo.Views.FirstPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CaliburnMicroDemo.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="FirstPage">
<Grid>
<Button x:Name="ChangeMessage" Content="Press Me" VerticalAlignment="Top" />
<TextBlock x:Name="Message" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding Path=Message, Mode=OneWay}"/>
</Grid>
</Page>
ShellViewModel.cs
public class ShellViewModel : PropertyChangedBase
{
}
FirstPageViewModel.cs
public class FirstPageViewModel: PropertyChangedBase
{
private string message;
public string Message
{
get
{
return message;
}
set
{
message = value;
NotifyOfPropertyChange(() => Message);
}
}
private int _pressCount;
public FirstPageViewModel()
{
Message = "Yolo";
_pressCount = 0;
}
public void ChangeMessage()
{
_pressCount++;
Message = "Presses = " + _pressCount;
}
}
现在这里的内容显示没有任何问题。但是当我点击按钮时,即使我通过NotifyOfPropertyChange 在FirstPageViewModel.cs 的设置器上映射它们,它也不起作用。
对于使用两个不同窗口的那个,答案是here。但是对于使用页面的人不知道该怎么做。
我的问题再次是如何映射 ViewModel 和 View 并绑定数据和更改属性。在这种情况下,在单击按钮时更改它。
如果我只有 ShellView 和 ShellViewModel,上述逻辑可以正常工作。我从 Bootstrapper 类开始,这是使用 Caliburn.micro 的标准方法。
如果有人可以帮助我解决这个问题,将不胜感激。需要更多详细信息,请发表评论会相应更新。
【问题讨论】:
-
First Page 和 First PageViewModel 之间的绑定在哪里?何时需要从 First PageViewModel 调用方法?
-
@Anton 编辑了答案。希望这能给你足够的信息
-
@joekevinrayan96:你的问题到底是什么?你想做什么?
-
@mm8 NotifyOfPropertychange 不起作用
标签: .net wpf mvvm data-binding caliburn.micro