开发者

Display a custom login control in Windows Phone with Mvvm light

开发者 https://www.devze.com 2023-04-08 05:13 出处:网络
Ok, so what I am looking to do is to display some sort of login control (maybe a UserControl with a TextBox and PasswordBox) when the app is started.

Ok, so what I am looking to do is to display some sort of login control (maybe a UserControl with a TextBox and PasswordBox) when the app is started.

In a non-mvvm situation, a way of doing this would be to use the PopUp primitive control, add the usercontrol as a child element and off you go.

In an MVVM situation, i'm a bit confused about how you would achieve a simmilar result.

I have looked into messaging with the DialogMessage and this is fine for displaying a typical Mess开发者_运维百科ageBox, but what about a custom usercontrol?

any help would be fantastic! I can't seem to find any demo code of this anywhere.


In a MVVM situation you can use a delegate to let your View open the dialog when the ViewModel requests it.

You define a delegate at the VM:

public Func<LoginResult> ShowLoginDialogDelegate;

In your View you define the function that will be called:

private LoginResult ShowLoginDialog()
{
    LoginResult result;
    // show a dialog and get the login data
    return result;
}

Then you "connect" the delegate and method in the View:

_viewModel = new MyViewModel();
DataContext = _viewModel;
_viewModel.ShowLoginDialogDelegate += ShowLoginDialog;

And now you can use it in your ViewModel e.g. when a command is executed like that:

LoginResult result = ShowLoginDialogDelegate();


An easier answer is to control it's visibility through a View State which with a little manipulation can be made to work through databinding allowing the view model to display the "Logon Page" state when required.

I just recently wrote about this for the Silverlight/XNA series which you can view here.

It would be much simplier if the SL4 DataEventrigger was available but hay ho.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号