点击按钮期间验证

时间:2012-09-20 16:55:25

标签: validation mvvm silverlight-4.0 command

我有按钮:

<sdk:Frame Margin="0,37,0,-15"     
                   Source="/View/Login/LogIn.xaml">
            <navigation:Frame.UriMapper>
                <uriMapper:UriMapper>
                    <uriMapper:UriMapping Uri="/Login" MappedUri="/View/Login/LogIn.xaml" />
                    <uriMapper:UriMapping Uri="/Home" MappedUri="/View/Home.xaml" />
                    <uriMapper:UriMapping Uri="/AddPayment" MappedUri="/View/AddPayment.xaml" />
                    <uriMapper:UriMapping Uri="/Reports" MappedUri="/View/Reports.xaml" />
                    <uriMapper:UriMapping Uri="/Admin" MappedUri="/View/Admin.xaml" />
                    <uriMapper:UriMapping Uri="/Init" MappedUri="/View/Init.xaml" />
                </uriMapper:UriMapper>
            </navigation:Frame.UriMapper>
        </sdk:Frame>
 <HyperlinkButton Grid.Row="2" Grid.Column="1" Content="Log in"  NavigateUri="/Home" Command="{Binding LoginCommand, NotifyOnValidationError=True}"/>

并查看模型:

 public RelayCommand LoginCommand
        {
            get
            {
                return this.loginCommand = new RelayCommand(param => this.LoginExecute(), param => (bool)this.CanLoginCommand);
            }
        }

        private object CanLoginCommand
        {
            get
            {
                return true;
            }
        }

        private void LoginExecute()
        {
            UserBag users = IsolatedStorageCacheManager<UserBag>.Retrieve(typeof(UserBag));
            if (users != null && users.Users != null && users.Users.Any(u => u.UserName == this.login && u.Password == this.password))
            {

            }
            else
            {
                throw new Exception("Bad password");
            }
        }

我需要当命令返回异常时它将返回到视图并显示,然后导航uri将不会被调用。 有可能吗?

1 个答案:

答案 0 :(得分:0)

您可以使用此静态类管理ViewModel中的导航:

public static class NavigationService
{
    private static Frame _frame;
    private static Frame Instance
    {
        get
        {
            if (_frame == null)
                throw new Exception("You must set the instance first");

            return _frame;
        }
    }


    public static void Navigate(string url)
    {
        Instance.Navigate(new Uri(url, UriKind.Relative));
    }

    public static void SetInstance(Frame frame)
    {
        _frame = frame;
    }

    public static void GoBack()
    {
        Instance.GoBack();
    }
}

在MainPage.xaml.cs中:

NavigationService.SetInstance(this.ContentFrame); // ContentFrame = your navigation Frame

现在在ViewModel中:

private void LoginExecute()
    {
        UserBag users = IsolatedStorageCacheManager<UserBag>.Retrieve(typeof(UserBag));
        if (users != null && users.Users != null && users.Users.Any(u => u.UserName == this.login && u.Password == this.password))
        {
            NavigationService.Navigate("/Home");
        }
        else
        {
            throw new Exception("Bad password");
        }
    }

您必须删除HyperlinkBut​​ton中的NavigateUri ;)