【问题标题】:How to bind multiple eventhandlers on WindowsPhone 8 using XAML?如何使用 XAML 在 WindowsPhone 8 上绑定多个事件处理程序?
【发布时间】:2014-07-09 06:19:20
【问题描述】:

我即将制作一个包含多个文本块的控制页面。 每个文本块都将“链接”到另一个页面,

这是我的 xaml 部分

.........<phone:LongListSelector x:Name="settingSelector" ItemsSource="{Binding}">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <StackPanel toolkit:TiltEffect.IsTiltEnabled="True" Tap="{Binding TapMethod, Mode=TwoWay}" >
                            <TextBlock Text="{Binding SetTitle}" FontSize="43" />
                            <TextBlock Text="{Binding SetDescription}" FontSize="19" Margin="0 0 0 10" />
                        </StackPanel>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>..........

当我在后面尝试这段代码时:

  .........public class SettingProperty
    {

        public string SetTitle{get; set;}
        public string SetDescription{get; set;}
        public string TapMethod{get; set;}
        public SettingProperty(string setTitle, string setDescription, string tapMethod)
        {
            SetTitle = setTitle;
            SetDescription = setDescription;
            TapMethod = tapMethod;
        }
    }

    List<SettingProperty> DataSetting()
    {
        List<SettingProperty> settingCollection = new List<SettingProperty>();
        settingCollection.Add(new SettingProperty("Saldo", "cek saldo", "saldo_Tap"));
        return settingCollection;
    }

    private void saldo_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        NavigationService.Navigate(new Uri("/saldo.xaml", UriKind.Relative));
    }..........

我直接将它们部署到我的 L520,我很确定罪魁祸首是我的堆栈面板上的“Tap”绑定,当我省略它时,代码可以工作。 是我遗漏了什么,还是我的整个方法都错了?

【问题讨论】:

  • 为什么要绑定一个字符串到 Tap 事件?

标签: c# xaml windows-phone-8


【解决方案1】:

我们对“Tap”事件的理解是错误的。

这里有两个选择:

  • 您要么明确指定一个方法名称(不是字符串)作为 Tap 事件的事件处理程序。例如:

    Tap="MyControl_Tap" 在这种情况下,您的控件代码中必须有一个 MyControl_Tap 方法

  • 或者,由于您似乎使用的是 MVVM 模式,因此您必须创建一个 ICommand,然后将整个 StackPanel 包含在一个 Button 中,如下所示:

    ...<DataTemplate>
    <Button Command="{Binding MyCommandProperty}">
                        <StackPanel toolkit:TiltEffect.IsTiltEnabled="True" >
                            <TextBlock Text="{Binding SetTitle}" FontSize="43" />
                            <TextBlock Text="{Binding SetDescription}" FontSize="19" Margin="0 0 0 10" />
                        </StackPanel>
    </Button>
    

    ...

【讨论】:

  • 感谢您的回答,我明白了您的想法,但我最终使用了来自的字符串:string selectedEvent = ((SettingProperty)((FrameworkElement)sender).DataContext).TapMethod;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
相关资源
最近更新 更多