【问题标题】:Combobox Binding in Windows Store using Caliburn使用 Caliburn 在 Windows 应用商店中绑定组合框
【发布时间】:2013-03-07 04:18:26
【问题描述】:

我正在尝试使用 Caliburn 和以下代码绑定组合框:

yyyView.xaml

 <ComboBox x:Name="Filters"></ComboBox>

yyyViewModel.xaml

private string selectedFilter;

public BindableCollection<string> Filters
{
    get
    {
        return new BindableCollection<string>(
                 new string[]{ "All", "Last Month", "Last Week", "Yesterday" });
    }
}

public string SelectedFilter
{
    get { return selectedFilter; }
    set
    {
        selectedFilter = value;
        NotifyOfPropertyChange(() => SelectedFilter);
    }
}

使用此代码,我在 App.xaml.cs 上收到 ArgumentNullException strong>GetInstance 方法。

我是 MVVM、Caliburn 和 XAML 的新手,但我在某处读到过某些行为(我相信是混合行为)在 WinRT 开发中下降。

是这个问题吗?我该如何解决这个问题?

谢谢

编辑:

App.xaml.cs

protected override void Configure()
{
    LogManager.GetLog = type => new DebugLogger(type);
    container = new WinRTContainer();
    container.RegisterWinRTServices();
    container.PerRequest<aaaViewModel>();
    container.PerRequest<xxxViewModel>();
    container.PerRequest<yyyViewModel>();
    container.PerRequest<zzzViewModel>();
}

App.xaml

<caliburn:CaliburnApplication
    x:Class="yyyStoreApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:yyyApp"
    xmlns:caliburn="using:Caliburn.Micro"
    xmlns:converters="using:yyyApp.Converters"
    RequestedTheme="Light">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/CustomStyles.xaml" />
                <ResourceDictionary Source="Resources/StandardStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <converters:ImageFilePathConverter x:Key="ImageFilePath"/>
        </ResourceDictionary>
    </Application.Resources>
</caliburn:CaliburnApplication>

【问题讨论】:

  • 你能发布你的 App.xaml 和任何相关的代码隐藏吗?听起来有些东西没有正确构建,尽管您的 Caliburn 绑定。
  • 我没有为此视图更改 App.xaml,并且该应用程序的其他视图正在运行。在后面的代码中我已经注册了 ViewModel:container.PerRequest&lt;yyyViewModel&gt;(); 我可以进入视图并查看其他绑定的元素,只有组合框无法正常工作。它是空的,当我单击它时会引发异常。

标签: mvvm windows-8 windows-store-apps winrt-xaml caliburn.micro


【解决方案1】:

您可能没有在App 实现中的Configure 方法重载中注册视图模型。请查看WinRT documentationApp 代码中的// TODO 部分。

这显然是有意的行为,但引起了一些混乱,如 CodePlex 上的讨论论坛和问题跟踪器所示,请参阅 hereherehere

基本上,只要将这一行添加到Configure 方法中,您就应该会很顺利:

container.PerRequest<yyyViewModel>();

【讨论】:

  • 感谢您的回答,但是我已经在App上注册了ViewModel。我什至在同一个视图上还有其他正在工作的绑定元素。问题一定是别的。抱歉,如果我没有说清楚:当我单击组合框时会出现问题。而且它开始是空的
  • @Silva 我已根据您指定的内容实现了您的代码,但我无法重现您的错误。我看到的唯一情况是ComboBox 项目无法正确显示开箱即用,因此我在ComboBox 中添加了ItemTemplate&lt;ComboBox.ItemTemplate&gt;&lt;DataTemplate&gt;&lt;TextBlock Text="{Binding}"/&gt;&lt;/DataTemplate&gt;&lt;/ComboBox.ItemTemplate&gt;。不过,老实说,我认为这对您的情况没有帮助,因此请发布您的 App.xaml.cs 的相关部分(例如 ConfigureOnLaunched 方法)以促进错误跟踪。
  • @Gustafsson,我已经更新了最初的帖子。 P.S.:我已经设法使用显式绑定来完成这项工作,即:&lt;ComboBox x:Name="Filters" ItemsSource="{Binding Filters}" SelectedValue="{Binding SelectedFilter, Mode=TwoWay}"/&gt; 但是,我相信 Caliburn 应该为我这样做......
  • @Silva 感谢您的代码。是的,CM 应该通过约定管理这些绑定,并且我的代码(与您的代码相同)按预期工作。你用的是哪个CM版本?您能否也发布您的OnLaunched 方法?
猜你喜欢
  • 1970-01-01
  • 2012-11-01
  • 2014-01-11
  • 2023-03-27
  • 2015-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多