【问题标题】:Calling another ViewModel using Caliburn.micro in .NET WPF在 .NET WPF 中使用 Caliburn.micro 调用另一个 ViewModel
【发布时间】: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


【解决方案1】:

您应该更改一些文件,如下所示。您需要一个引导程序,并且不需要 FirstPage.xaml.cs 和 ShellView.xaml.cs 文件。

第一页.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" 
  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"/>
</Grid>

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"
    mc:Ignorable="d"
    Title="ShellWindow" Height="450" Width="800">
<Grid>
    <Frame x:Name="ActiveItem" NavigationUIVisibility="Hidden"/>
</Grid>

第一页视图模型:

using Caliburn.Micro;

namespace CaliburnMicroDemo.ViewModels;

public class FirstPageViewModel : PropertyChangedBase
{
    private string message = "Yolo";

    public string Message
    {
        get => message;
        set
        {
            message = value;
            NotifyOfPropertyChange(() => Message);
        }
    }

    private int _pressCount = 0;

    public void ChangeMessage()
    {
        _pressCount++;
        Message = "Presses = " + _pressCount;
    }
}

外壳视图模型:

using Caliburn.Micro;

namespace CaliburnMicroDemo.ViewModels;

public class ShellViewModel : Conductor<object>
{
    public ShellViewModel()
    {
        ShowFirstPage();
    }

    public void ShowFirstPage()
    {
        _ = ActivateItemAsync(new FirstPageViewModel());
    }
}

应用程序.xaml:

<Application x:Class="CaliburnMicroDemo.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:CaliburnMicroDemo">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary>
                <local:HelloBootstrapper x:Key="bootstrapper" />
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

HelloBootstrapper.cs:

using Caliburn.Micro;
using CaliburnMicroDemo.ViewModels;
using System.Windows;

namespace CaliburnMicroDemo;

public class HelloBootstrapper : BootstrapperBase
{
    public HelloBootstrapper()
    {
        Initialize();
    }

    protected override void OnStartup(object sender, StartupEventArgs e)
    {
        _ = DisplayRootViewForAsync<ShellViewModel>();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 2020-05-19
    相关资源
    最近更新 更多