首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActivateItemAsync in Caliburn.Micro

ActivateItemAsync in Caliburn.Micro
EN

Stack Overflow用户
提问于 2022-08-26 20:39:16
回答 1查看 92关注 0票数 0

最近,我开始研究MVVM,在其中我正在研究Caliburn.Micro框架。不幸的是,我只能看到非常老的内容,框架文档已经过时了。我使用的是杯标4.0.173,它不再有被ActivateItemAsync取代的ActivateItemAsync方法,请按照下面的代码: ShellViewModel.cs。

ShellViewModel.cs

代码语言:javascript
复制
        public async void LoadPageOne()
        {
            await ActivateItemAsync(new FirstChildViewModel(), CancellationToken.None);
        }

        public async void LoadPageTwo()
        {
            await ActivateItemAsync(new SecondChildViewModel(), CancellationToken.None);
        }

ShellView.Xaml

代码语言:javascript
复制
        <!-- Row 5 -->
        <Button x:Name="LoadPageOne" Grid.Row="5" Grid.Column="1"> Load First Page</Button>
        <Button x:Name="LoadPageTwo" Grid.Row="5" Grid.Column="2"> Load Second Page</Button>

        <!-- Row 6 -->
        <ContentControl Grid.Row="6" Grid.Column="1" Grid.ColumnSpan="5" x:Name="ActiveItem"/>

在视频中,他使用的是dotnet框架4.6和3.2中的杯本,而我使用的是dotnet 6。即使添加了我想要找到的所有内容,即使是在github上,用户控制屏幕也不会改变。谁能告诉我我把它放哪儿了吗?我是一个初级程序员,我想了解这个问题,而不是把所有的东西都改到以前的版本。

EN

回答 1

Stack Overflow用户

发布于 2022-10-21 20:45:05

我试过了,效果很好。

如果您想使用内置于IoC中并希望在视图模型之间添加消息传递,则可以签出我的样本项目

ViewModels:

代码语言:javascript
复制
using Caliburn.Micro;
using System.Threading.Tasks;

namespace CaliburnMicroDemo.ViewModels;

public class ShellViewModel : Conductor<IScreen>
{
    public async Task LoadPageOneAsync()
    {
        await ActivateItemAsync(new FirstChildViewModel());
    }

    public async Task LoadPageTwoAsync()
    {
        await ActivateItemAsync(new SecondChildViewModel());
    }
}

public class FirstChildViewModel : Screen
{
}

public class SecondChildViewModel : Screen
{
}

ShellView.xaml:

代码语言:javascript
复制
<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="Shell Window" Height="450" Width="800">
    <DockPanel>
        <Button x:Name="LoadPageOneAsync" Content="Load First Page" DockPanel.Dock="Top"/>
        <Button x:Name="LoadPageTwoAsync" Content="Load Second Page" DockPanel.Dock="Top"/>
        <ContentControl x:Name="ActiveItem" DockPanel.Dock="Bottom"/>
    </DockPanel>
</Window>

FirstChildView.xaml:

代码语言:javascript
复制
<UserControl x:Class="CaliburnMicroDemo.Views.FirstChildView"
  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">
    <Grid>
        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="1"/>
    </Grid>
</UserControl>

SecondChildView.xaml:

代码语言:javascript
复制
<UserControl x:Class="CaliburnMicroDemo.Views.SecondChildView"
  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">
    <Grid>
        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="2"/>
    </Grid>
</UserControl>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73506270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档