为什么我的invokecommandaction不会触发? [英] Why could my invokecommandaction not fire?

查看:149
本文介绍了为什么我的invokecommandaction不会触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。点击文本块后,我想在我的视图模型中提升其内容,如果准确,则导航到另一个页面。不幸的是,click事件永远不会触发其绑定命令。以下是我的代码



我尝试过:



在xaml,我有这个标记

 <   UserControl     x:Class   =  SchoolPrism.Login.Views.Login  

xmlns = http://schemas.microsoft.com/winfx/2006/xaml / presentation

< span class =code-attribute> xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml

xmlns:mc = http://schemas.openxmlformats.org/markup-compatibility/2006

xmlns:i = clr-namespace:System.Windows.Interactivity; assembly = System.Windows.Interactivity

xmlns:d = http://schemas.microsoft.com/expression/blend/2008

xmlns:local = clr-namespace:SchoolPrism.Login.Views

< span class =code-attribute> xmlns:prism = http://prismlibrary.com/

xmlns:constants = clr-namespace:Infrastructure.Constants ; assembly = SchoolPrismInfrastructure

< span class =code-attribute> xmlns:login = clr-namespace:SchoolPrism.Login.ViewModel

< span class =code-attribute> mc:Ignorable = d

d:DesignHeight = 300 d:DesignWidth = 300 >

<! - 当我们有一个视图模型时,绑定到那个 - >
< 网格 DataContext = {Binding login:LoginViewModel} >
< 网格.RowDefinitions >
< RowDefinition / >
< RowDefinition 高度 = 5 * / >
< / Grid.RowDefinitions >

< TextBlock DataContext = {Binding} 文字 = {Binding FormResults} > < / TextBlock >

< TextBlock 文字 = 你能看到我吗? x:姓名 = SchoolCode Grid.RowSpan = 2 保证金 = 0,50,0,0 >
< i:Interaction.Triggers >
< i:EventTrigger EventName = PreviewMouseDown >

<! - 尝试用pr替换交互性ism here但没有效果 - >
< i:InvokeCommandAction 命令 < span class =code-keyword> =
{Binding submitForm} CommandParameter = {Binding Text,ElementName = SchoolCode} > < / i:InvokeCommandAction >
< / i:EventTrigger >
< / i:Interaction.Triggers >
< < span class =code-leadattribute> / TextBlock >
< / Grid >
< / UserControl >





然后,在视图模型中,我有这个



 使用 Prism.Commands; 
使用 Prism.Modularity;
使用 SchoolPrism.Modules.Login;
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Input;

namespace SchoolPrism.Login.ViewModel
{
class LoginViewModel
{

ICommand submitForm
{
get { return new DelegateCommand< string>(SubmitForm); }
}

public void SubmitForm( string 代码)
{

// 在给定代码上与模型交互

// 如果是auth,则获取模块实例并调用以下
new LoginModule()。proceedToNext( AllTopics);

}
}
}





最后,在模块中,我有




 使用 Microsoft。 Practices.ServiceLocation; 
使用 Microsoft.Practices.Unity;
使用 Prism.Commands;
使用 Prism.Modularity;
使用 Prism.Mvvm;
使用 Prism.Regions;
使用 SchoolPrism.Login.Views;
使用系统;
使用 System.Windows;
使用 System.Windows.Input;

namespace SchoolPrism.Modules.Login
{
[Module(ModuleName = LoginModule)]
class LoginModule:BindableBase,IModule
{

IRegionManager _regionMan;

// 此方法由prism为我们调用
public void Initialize()
{

_regionMan = ServiceLocator .Current.GetInstance< iregionmanager>();

}

public void proceedToNext(< span class =code-keyword> string target)
{

_regionMan.RequestNavigate( LoginForm new Uri(target,UriKind.Relative) / * ,(NavigationResult result)=> {return code.Length> 0;} * / );
}
}
}



请问,我做错了什么?

解决方案

使用



 <   Grid.DataContext  >  
< < span class =code-leadattribute> login:LoginViewModel > < / login:LoginViewModel >
< / Grid.DataContext >





解决了它


尝试像这样设置 DataContext

< UserControl.Resources> 
< login:LoginViewModel x:Key = loginvm />
< / UserControl.Resources >
< UserControl.DataContext>
< Binding Source = {StaticResource loginvm} />
< / UserControl.DataContext >


Hello there. On click of a textblock, I want to lift its contents in my view model and if accurate, navigate to another page. Unfortunately for me, the click event never triggers its bound command. Below is my code

What I have tried:

In the xaml, I have this markup

<UserControl x:Class="SchoolPrism.Login.Views.Login"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

             xmlns:local="clr-namespace:SchoolPrism.Login.Views"

             xmlns:prism="http://prismlibrary.com/"

             xmlns:constants="clr-namespace:Infrastructure.Constants;assembly=SchoolPrismInfrastructure"

             xmlns:login="clr-namespace:SchoolPrism.Login.ViewModel"

             mc:Ignorable="d" 

             d:DesignHeight="300" d:DesignWidth="300">

    <!-- when we have a view model, bind to that instead-->
    <Grid DataContext="{Binding login:LoginViewModel}">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="5*"/>
        </Grid.RowDefinitions>

        <TextBlock DataContext="{Binding}" Text="{Binding FormResults}"></TextBlock>

        <TextBlock Text="hi, can you see me?" x:Name="SchoolCode" Grid.RowSpan="2" Margin="0,50,0,0">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="PreviewMouseDown">
                    
<!--tried replacing interactivity with prism here but it had no effect -->
<i:InvokeCommandAction Command="{Binding submitForm}" CommandParameter="{Binding Text, ElementName=SchoolCode}" ></i:InvokeCommandAction>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TextBlock>
    </Grid>
</UserControl>



Then, in the view model, I have this

using Prism.Commands;
using Prism.Modularity;
using SchoolPrism.Modules.Login;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace SchoolPrism.Login.ViewModel
{
    class LoginViewModel
    {

        ICommand submitForm
        {
            get { return new DelegateCommand<string>(SubmitForm); }
        }

        public void SubmitForm(string code)
        {

            // interact with model over given code

            // if auth, get module instance and invoke the following
            new LoginModule().proceedToNext("AllTopics");

        }
    }
}



Finally, in the module proper, I have


using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using Prism.Commands;
using Prism.Modularity;
using Prism.Mvvm;
using Prism.Regions;
using SchoolPrism.Login.Views;
using System;
using System.Windows;
using System.Windows.Input;

namespace SchoolPrism.Modules.Login
{
    [Module(ModuleName = "LoginModule")]
    class LoginModule : BindableBase, IModule
    {

        IRegionManager _regionMan;

        // this method is called on our behalf by prism
        public void Initialize ()
        {

            _regionMan = ServiceLocator.Current.GetInstance<iregionmanager>();

        }

        public void proceedToNext (string target)
        {

            _regionMan.RequestNavigate("LoginForm", new Uri(target, UriKind.Relative)/*, (NavigationResult result) => { return code.Length > 0; }*/);
        }
        }
 }


Please, what am I doing wrong?

解决方案

Using

<Grid.DataContext>
  <login:LoginViewModel>    </login:LoginViewModel>
</Grid.DataContext>



Solved it


Try setting the DataContext like this

<UserControl.Resources>
        <login:LoginViewModel x:Key="loginvm" />
    </UserControl.Resources>
    <UserControl.DataContext>
        <Binding Source="{StaticResource loginvm}" />
</UserControl.DataContext>


这篇关于为什么我的invokecommandaction不会触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆