简体   繁体   English

在View中切换xaml的一部分(Caliburn.Micro)

[英]Switching part of xaml in View (Caliburn.Micro)

I'd like to switch only some part of my View (which is UserControl) xaml. 我只想切换View(即UserControl)xaml的一部分。 For example, I'd like to be able to change only 2nd Grid. 例如,我只想更改第二网格。

<Grid> //main grid
    <Grid Name="1" Grid.Row="1"/>
    <Grid Name="2" Grid.Row="2"/>
</Grid

I've tried sth like this: 我已经尝试过这样的事情:

<UserControl.Resources>
    <ControlTemplate x:Key="UsualMode">
        <Grid>
        ...
        </Grid>
    </ControlTemplate>
</UserControl.Resources>
<Grid> //main grid
    <Grid Name="1" Grid.Row="1"/>
    <ControlTemplate Name="2" Grid.Row="2" Template="{StaticResource UsualMode}"/>
</Grid>

Then, by using triggers and binding I would be able to switch templates. 然后,通过使用触发器和绑定,我将能够切换模板。 Unfortunately, this doesn't work for me due to 'Bootstrapper.cs not found' exception. 不幸的是,由于'Bootstrapper.cs not found'异常,这对我不起作用。 How should I do that? 我应该怎么做? I cannot use conductor -> have to load only one View. 我无法使用导体->仅必须加载一个视图。

http://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Conventions http://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Conventions

Read up on the basics of view resolution 阅读有关视图分辨率的基础知识

Basically you would create the following in your view: 基本上,您将在视图中创建以下内容:

<UserControl.Resources>
    <ControlTemplate x:Key="UsualMode">
        <Grid>
        ...
        </Grid>
    </ControlTemplate>
</UserControl.Resources>
<Grid> //main grid
    <Grid Name="1" Grid.Row="1"/>
    <ContentControl x:Name="ChildViewModel" cal:View.Context="{Binding ContextBinding}" />
</Grid>

Your parent viewmodel needs to have a 'context' property and a property to house the child VM: 您的父视图模型需要具有“上下文”属性和用于容纳子VM的属性:

public class ParentViewModel
{
    public SomeViewModel ChildViewModel { get; private set; }

    public string ContextBinding { get; private set; } // make sure you implement INPC on these properties as is the usual
}

Your view will then be resolved based on the ContextBinding string (as per the CM conventions above). 然后,将根据ContextBinding字符串(按照上述CM约定)解析您的视图。

So if you were to update the string: 因此,如果您要更新字符串:

ContextBinding = "DetailedView";

CM would then update the UI and try to look for a view called DetailedView in a subnamespace of the current VMs namespace 那么CM将更新用户界面,并尝试寻找一个叫做视图DetailedView在当前虚拟机的命名空间的子名字空间

If you don't want to have a child VM, you can actually get CMs conventions to kick in earlier and apply a context over the current VM, but in this case you would need to create two views which were almost identical apart from the area which you would like to 'swap out'. 如果您不想拥有子虚拟机,则实际上可以使CM约定更早生效,并在当前虚拟机上应用上下文,但是在这种情况下,您需要创建两个视图,除了该区域外几乎相同您想“交换”。

My preference would be to create a child VM to handle the sub-area that will swap views as I've shown above 我的偏好是创建一个子虚拟机来处理将交换视图的子区域,如我上面所示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM