簡體   English   中英

Caliburn.Micro將它綁定到MainView中的UserControls到他們的ViewModels

[英]Caliburn.Micro getting it to bind UserControls in a MainView to their ViewModels

我有一個MainView.xaml,綁定到MainViewModel就好了。

我想嘗試的是將我在主窗體上的許多控件分成UserControls。

現在我將UserControls與MainView一起放在Views文件夾中,並命名為LeftSideControlView.xaml和RightSideControlView.xaml。 相應的ViewModel位於ViewModels文件夾中,名為LeftSideControlViewModel等。

我成功將usercontrols添加到主視圖:

 <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <UserControls:LeftSideControlView cal:Bind.Model="{Binding}" />
    <UserControls:RightSideControlView cal:Bind.Model="{Binding}"
                                  Grid.Column="1" />
</Grid>

它們在設計師中正確顯示。 這是xaml中的其中一個:

 <UserControl x:Class="TwitterCaliburnWPF.Library.Views.LeftSideControlView"
         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="300" d:DesignWidth="300">
<Grid>
    <StackPanel>
        <Label x:Name="Text" FontSize="25" HorizontalAlignment="Center" Margin="5"/>
        <TextBox x:Name="TextBox1"
                 Width="200"
                 HorizontalAlignment="Center" FontSize="25" Margin="5" />
        <Button Width="200" Height="50" Content="Button!" Margin="20" />
    </StackPanel>
</Grid>

我使用Castle.Windsor在AppBootstrapper for Caliburn中添加了viewmodels及其接口。

 public class ApplicationContainer : WindsorContainer
 {
  public ApplicationContainer()
  {
     // Register all dependencies here
     Register(
        Component.For<IWindowManager>().ImplementedBy<WindowManager>().LifeStyle.Is(LifestyleType.Singleton),
        Component.For<IEventAggregator>().ImplementedBy<EventAggregator>().LifeStyle.Is(LifestyleType.Singleton),
        Component.For<ILeftSideControlViewModel>().ImplementedBy<LeftSideControlViewModel>(),
        Component.For<IRightSideControlViewModel>().ImplementedBy<RightSideControlViewModel>()
        );

     RegisterViewModels();
  }

  private void RegisterViewModels()
  {
     Register(AllTypes.FromAssembly(GetType().Assembly)
                  .Where(x => x.Name.EndsWith("ViewModel"))
                  .Configure(x => x.LifeStyle.Is(LifestyleType.Transient)));
  }

這是LeftSideControlViewModel類:

 using Screen = Caliburn.Micro.Screen;

 namespace TwitterCaliburnWPF.Library.ViewModels
 {
    public class LeftSideControlViewModel : Screen, ILeftSideControlViewModel
    {
       private string _text = "Hello from the Left side!";
       private string _textBox1 = "Enter Text Here";

       public string Text
       {
          get { return _text; }
       }

       public string TextBox1
       {
          get { return _textBox1; }
       }
    }
 }

這是MainViewModel,我正在閱讀我在Caliburn.Micro文檔中讀到的內容,就像之前我嘗試過的那樣,MainViewModel中沒有任何內容告訴它加載這兩個控件或顯示這兩個控件。

仍然當應用程序啟動並運行時,值不會綁定到各自視圖模型中的用戶控件。

namespace TwitterCaliburnWPF.Library.ViewModels
{
   public class MainViewModel : Conductor<IScreen>
   {
      public MainViewModel()
      {
         ShowLeftControl();
         ShowRightControl();
      }

      private void ShowRightControl()
      {
         ActivateItem(new RightSideControlViewModel());
      }

      private void ShowLeftControl()
      {
         ActivateItem(new LeftSideControlViewModel());
      }

      public string TextToDisplay
      {
         get { return "Coming from the ViewModel!"; }
      }
   }
}

您不需要在這里使用Conductor 這基本上用於導航場景。 只是要兩個公共屬性您MainViewModel一個RightSideControlViewModel叫RightSide,一個用於LeftSideControlViewModel ,叫萊夫特賽德。 然后,不是直接在MainView中實例化UserControl,而是創建兩個ContentControls ,一個用x:Name="LeftSide" ,另一個用x:name="RightSide"這是一個視圖模型第一種完成它的方法。 如果您想先查看視圖,請在MainView中保留用戶控件定義,但更改Bind.Model以使其指向您創建的新屬性,如Bind.Model="{Binding LeftSide}"

基本上,你有事物的方式定義....綁定只是或多或少指向正確的對象。 你有導體在那里,你不需要完成這個。 如果您打算使用某種導航架構,可能需要保留它。 請記住,當您在Conductor上調用ActivateItem時,您基本上正在更改其ActiveItem屬性; 只有一個模型一次處於活動狀態。 在上面的情況下,您激活兩個項目,但只有第二個項目保持活動狀態。 此外,在您的視圖中,ActiveItem沒有任何約束。

我希望這是有道理的!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM