简体   繁体   English

Caliburn.Micro的UWP约定

[英]UWP conventions with Caliburn.Micro

I have created a sample UWP project and added Caliburn.Micro 3.0 beta1 to it based on this tutorial . 我已经创建了一个示例UWP项目,并根据本教程在其中添加了Caliburn.Micro 3.0 beta1。

Using the SplitView, I could add a new ViewModel when a Pane button is pressed: 使用SplitView,可以在按下“窗格”按钮时添加一个新的ViewModel:

ShellView ShellView

 <Button x:Name="OpenSecondView"
         Margin="24"
         Content="My Second View" />

ShellViewModel ShellViewModel

public void OpenSecondView()
{
  _navigationService.For<MainViewModel>().Navigate();
}

It loads the proper View (so I presume, conventions are working). 它加载了正确的View(所以我认为约定正在起作用)。

I have a button in the detail view and it has the Name property set. 我在详细信息视图中有一个按钮,并且它设置了Name属性。

MainView 的MainView

<Button Name="btnStartAction"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        RelativePanel.AlignHorizontalCenterWithPanel="True"
        RelativePanel.Below="btnInformation"
        Style="{StaticResource button}">

I added a method in the ViewModel with the same name. 我在ViewModel中添加了一个同名方法。

MainViewModel MainViewModel

public void btnStartAction()
{
  var x = 2;
}

Unfortunately when I click it, nothing happens. 不幸的是,当我单击它时,没有任何反应。

What should I set? 我应该设置什么?

UPDATE UPDATE

Well, something strange happened. 好吧,发生了一件奇怪的事。

If I add the following line in MainView.xaml.cs , the button works: 如果我在MainView.xaml.cs中添加以下行,则该按钮有效:

public MainView()
{
   this.InitializeComponent();
   this.DataContext = new MainViewModel();
}

So, it seems that the ViewModel can find its View (hence I can see it) but the View has no DataContext. 因此,似乎ViewModel可以找到其View(因此我可以看到它), 但是 View没有DataContext。

What should I do? 我该怎么办?

UPDATE 2 更新2

This is how the Frame gets registered. 这是注册框架的方式。

XAML XAML

 <SplitView.Content>
                <Grid>                  
                <Frame Grid.Row="1"
                           DataContext="{x:Null}"
                           cm:Message.Attach="[Event Loaded] = [SetupNavigationService($source)]" />
                </Grid>
  </SplitView.Content>

ShellModelView ShellModelView

public ShellViewModel(WinRTContainer container)
{
  _container = container;      
}

public void SetupNavigationService(Frame frame)
{
  _navigationService = _container.RegisterNavigationService(frame);
}

Caliburn Micro conventions feature use x:Name directive not Name property. Caliburn Micro约定功能使用x:Name指令而不是Name属性。

<Button x:Name="btnStartAction"
    HorizontalAlignment="Center"
    VerticalAlignment="Center"
    RelativePanel.AlignHorizontalCenterWithPanel="True"
    RelativePanel.Below="btnInformation"
    Style="{StaticResource button}">

Update 更新

Sorry for giving wrong information, both Name and x:Name can be used for conventions. 很抱歉提供错误的信息,名称和x:Name都可以用于约定。

You should register you MainViewModel to WinRTContainer . 您应该将MainViewModel注册到WinRTContainer

App.xaml.cs App.xaml.cs

public sealed partial class App
{
  private WinRTContainer _container;

  protected override void Configure()
  {
     _container = new WinRTContainer();
     _container.RegisterWinRTServices();

     _container
          .PerRequest<ShellViewModel>()
          .PerRequest<MyFirstViewModel>()
          .PerRequest<MySecondViewModel>()
          .PerRequest<MyThirdViewModel>()
          .PerRequest<MainViewModel>();

     var navigationManager = SystemNavigationManager.GetForCurrentView();

     navigationManager.AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;

  }
}

More about simple container see Simple IoC Container . 有关简单容器的更多信息,请参见简单IoC容器

Be aware that if you decide to implement suspend and restore operations that require cacheing of the viewmodel for later restoration that there will likely be an issue related the ShellView and Window.Current.Contents being set incorrectly to the restored view instead of the ShellView page. 请注意,如果您决定实施挂起和还原操作,这些操作需要缓存视图模型以用于以后的还原,则很可能会出现与ShellView和Window.Current.Contents设置不正确的还原视图(而不是ShellView页面)相关的问题。 Workaround in the works for this issue. 解决此问题的方法。

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

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