【问题标题】:XAML MenuFlyoutItem and command bindingXAML MenuFlyoutItem 和命令绑定
【发布时间】:2023-04-09 21:12:01
【问题描述】:

我想在点击我的“GridView”项目时添加一个“MenuFlyoutItem”。 为此,我编写了以下 xaml :

<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Holding">
    <utils:OpenMenuFlyoutAction />
</core:EventTriggerBehavior>

<FlyoutBase.AttachedFlyout>
<MenuFlyout>
    <MenuFlyoutItem x:Uid="LBL_DELETE" 
                    Text="" 
                    Command="{Binding OnDeleteCommand}" 
                    CommandParameter="{Binding}"/>
</MenuFlyout>

但我不希望绑定发生在项目视图模型上,而是发生在我的视图模型类上:“MainViewModel”。 所以我修改了这一行:

Command="{Binding OnDeleteCommand, Source={StaticResource MainVM}}" 

并将这一行添加为资源:

<viewModel:MainViewModel x:Key="MainVM"/>

它可以工作,但现在在我的编辑器中,我的所有数据模板定义都带有下划线,并且我收到以下消息:无法从文本“”创建“Windows.UI.Xaml.Media.Brush”。

截图在这里:http://hpics.li/9f17b6e

[编辑] 如果我定义文本属性,错误是“无法创建类型为 'Average.ViewModel.MainViewModel' 的实例”

我的代码有效,但我会删除此警告。 有谁知道我为什么会收到这条消息?

【问题讨论】:

  • 考虑 not 有一个空的MenuFlyoutItem -- 你需要指定Text 属性。
  • Text 属性在这里由全球化定义。 'LBL_DELETE.Text' 在我的'Resources.resw' 中定义。但是...我尝试直接添加Text属性,现在错误不一样了。错误是“无法创建 'Average.ViewModel.MainViewModel' 类型的实例”。所以,它给了我一个轨迹。

标签: xaml windows-phone-8.1 win-universal-app


【解决方案1】:

经过几天的寻找答案,我终于解决了这个问题......但没有详细解释。 事实上,我更新了我的一些应用程序包,其中包括: “仅限 MVVM 轻量级库” “通用服务定位器”

错误可能来自 MVVM Light 框架,但现在问题已得到纠正。

【讨论】:

    【解决方案2】:

    这是一个如何使用 MenuFlyOutItem 实现命令绑定的示例。 此实现利用了 Expression Blend 2013 中的行为。

    XAML:

    <HyperlinkButton
                    Content="{Binding SelectedItem.Name, ElementName=ContactList, Mode=OneWay}">
        <Interactivity:Interaction.Behaviors>
            <Core:EventTriggerBehavior EventName="Holding">
                <behaviors:MoveContactBehavior />
            </Core:EventTriggerBehavior>
        </Interactivity:Interaction.Behaviors>
    
                <FlyoutBase.AttachedFlyout>
            <MenuFlyout>
                <MenuFlyoutItem Text="Family" Command="{Binding MoveCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />
                <MenuFlyoutItem Text="Friends" Command="{Binding MoveCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />
                <MenuFlyoutItem Text="Business" Command="{Binding MoveCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />
            </MenuFlyout>
        </FlyoutBase.AttachedFlyout>
    </HyperlinkButton>
    

    类:

    public class MoveContactBehavior : DependencyObject, IAction
    {
        public object Execute(object sender, object parameter)
        {
            var senderElement = sender as FrameworkElement;
            FlyoutBase flyoutBase = FlyoutBase.GetAttachedFlyout(senderElement);
    
            flyoutBase.ShowAt(senderElement);
    
            return null;
        }
    }
    

    视图模型:

    public HomeViewModel()
    {
        MoveCommand = new DelegateCommand(MoveContact);
    }
    
    public DelegateCommand MoveCommand
    {
        get;
        private set;
    }
    
    private void MoveContact(object e)
    {
        var targetCategory = e as string;
        SelectedCategory.Contacts.Remove(SelectedContact);
    
        switch(targetCategory)
        {
            case "Family":
                {
                    FamilyCategory.Contacts.Add(SelectedContact);
                    break;
                }
    
            case "Friends":
                {
                    FriendsCategory.Contacts.Add(SelectedContact);
                    break;
                }
    
            case "Business":
                {
                    BusinessCategory.Contacts.Add(SelectedContact);
                    break;
                }
    
            default:
                {
                    throw new NotImplementedException();
                }
        }
    }
    

    委托命令:

    public class DelegateCommand : ICommand
    {
        Func<object, bool> canExecute;
        Action<object> executeAction;
    
        public DelegateCommand(Action<object> executeAction)
            : this(executeAction, null)
        {
        }
    
        public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecute)
        {
            if (executeAction == null)
            {
                throw new ArgumentNullException("executeAction");
            }
            this.executeAction = executeAction;
            this.canExecute = canExecute;
        }
    
        public bool CanExecute(object parameter)
        {
            bool result = true;
            Func<object, bool> canExecuteHandler = this.canExecute;
            if (canExecuteHandler != null)
            {
                result = canExecuteHandler(parameter);
            }
    
            return result;
        }
    
        public event EventHandler CanExecuteChanged;
    
        public void RaiseCanExecuteChanged()
        {
            EventHandler handler = this.CanExecuteChanged;
            if (handler != null)
            {
                handler(this, new EventArgs());
            }
        }
    
        public void Execute(object parameter)
        {
            this.executeAction(parameter);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-26
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      • 2014-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多