0

我使用 Caliburn Micro 为 Windows Phone 7 开发应用程序。

听到的是应用程序主要部分的代码。

MainView的一部分:

<Grid x:Name="LayoutRoot" Background="Transparent">
    <controls:Panorama>
        <controls:PanoramaItem x:Name="SubPanoramaItem"
                               DataContext="{Binding SubViewModel}">
            <StackPanel>

                <toolkit:ListPicker ExpansionMode="FullScreenOnly" 
                                    ItemsSource="{Binding DataModeList}">

                    <toolkit:ListPicker.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Tag}" />
                        </DataTemplate>
                    </toolkit:ListPicker.ItemTemplate>

                    <toolkit:ListPicker.FullModeItemTemplate>
                        <DataTemplate>

                            <StackPanel x:Name="item"
                                        Margin="5, 24, 0, 24"
                                        cal:Action.TargetWithoutContext="{Binding ElementName=SubPanoramaItem,
                                                                                  Path=DataContext}"
                                        cal:Message.Attach="[Event Tap] = [Action Tap($dataContext)]"
                                        Orientation="Horizontal">

                                <TextBlock FontSize="40" 
                                           Text="{Binding PopupText}" />
                            </StackPanel>

                        </DataTemplate>
                    </toolkit:ListPicker.FullModeItemTemplate>

                </toolkit:ListPicker>

            </StackPanel>
        </controls:PanoramaItem>

        <!-- Some other code -->
    </controls:Panorama>
</Grid>

主视图模型

public class MainViewModel: Screen
{
    public SubViewModel SubViewModel { get; private set; }

    public MainViewModel(SubViewModel subViewModel)
    {
        SubViewModel = subViewModel;
    }

    // some other code
}

子视图模型

public class SearchViewModel : Screen
{
    private ObservableCollection<DateModeItem> _dataModeList = 
        new ObservableCollection<DateModeItem>()
            {
                new DataItem
                { PopupText = "Item 1" },
                new DataItem
                { PopupText = "Item 2" },
                new DataItem
                { PopupText = "Item 3" },
                new DataItem
                { PopupText = "Item 4" }  
            };

    public ObservableCollection<DateModeItem> DataModeList
    {
        get
        {
            return _dataModeList;
        } 

        private set { _dataModeList = value; }
    }

    public void Tap(object dataContext)
    {
        var item = dataContext as DataItem;
        if (item != null)
        {
            var r = new Random();
            switch (item.PopupText)
            {
                case "Item 1":
                    item.Tag = r.Next(5);
                    break;
                case "Item 2":
                    item.Tag = r.Next(5, 10);
                    break;
                case "Item 3":
                    item.Tag = r.Next(10, 15);
                    break;
                case "Item 4":
                    item.Tag = r.Next(15, 20);
                    break;
            }
        }
    }
}

数据项

public class DataItem 
{
    public string PopupText { get; set; }
    public int Tag { get; set; }
}

如您所见,我已将 Action 附加到 ListPicker 中 DataTemplate 的每个 StackPanel。当点击列表中的项目时,必须生成新的随机标签。这个标签被插入到 ListPicker 的文本框中。

而且这个动作表现得很奇怪。当我点击 1、2 和 4 项目时,什么也没有发生。当我点击 3 项时,应用程序抛出异常 - “没有找到方法 Tap 的目标”。当我使用 Silverlight Toolkit 中的 ListPicker 时会发生这种情况。

我还从 Telerik 的 RadConrols 库中试用了 RadListPicker。当我使用它时,动作方法的调用是不可预测的。有时动作调用正确的方法。有时什么都不会发生。我可以肯定地说 - 点击最后一项,它以正确的方式工作的频率降低。

到底是怎么回事?我无法理解。

附加信息:

我已经清理了我的应用程序中所有不必要的东西,只留下了我在上一篇文章中描述的代码。

现在,当我使用 ListPicker 时 - 什么都没有发生。列表在点击时没有响应。有时应用程序会抛出“No target found for method Tap”异常。当我使用 RadListPicker 时 - 几乎总是未调用操作,有时(很少)正确调用。

4

1 回答 1

1

当您使用 ListPicker 时,您需要添加一些 ElementConventions 以便将您的视图的操作绑定到您的视图模型。

添加绑定约定可以在 AppBootstrapper 类中完成。代码可能类似于:

    protected override void Configure()
    {
      ConventionManager.AddElementConvention<ListPicker>(ListPicker.ItemsSourceProperty, "SelectedItem", "SelectionChanged").ApplyBinding = 
        (viewModelType, path, property, element, convention) => {
         if (ConventionManager.GetElementConvention(typeof(ItemsControl)).ApplyBinding(viewModelType, path, property, element, convention))
         {
             ConventionManager.ConfigureSelectedItem(element, ListPicker.SelectedItemProperty, viewModelType, path);
             return true;
         }
         return false;
     }; }
于 2013-07-03T20:41:32.233 回答