简体   繁体   中英

Using controls with functionality with MVVM WP8 c#/xaml

let's take an example. Class WebBrowserTask provides us to open specified URL in control. We define it in our view. How to get reference to that control in our viewmodel? Should we create it in our view and use "webBrowserTaskInstance.Show()" in code behind? Or is it possible to use "Show()" function in MVVM pattern?

Edit: Example with WebBrowserTask was wrong. Let's take another example:

        public class MainViewModel
        {
              private IInternetService _internetService;

                   public MainViewModel(IInternetService internetService)
                   {
                    _internetService = internetService;
                   }

              RelayCommand ComputeCommand
              {
                 get
                 {
                  blablabla -> _internetService.Compute();
                 }
              }

        }

And now in my MainView.xaml:

<namespace:InternetControl x:Name="MyControl" />

Let's say that InternetControl have a function Compute() and single Textbox. After invoking Compute() it takes something from internet and writes into that TextBox.

I want my ComputeCommand to invoke specified service( in here IInternetService), which implementation wrap my control and invoke Compute on it - For example:

public class InternetService : IInternetService
{
       InternetControl internetControl; // how to spare reference to it with my control in view?

  public void Compute()
  {
   internetControl.Compute();
  }
}
  1. How to connect these references?
  2. In my opinion InternetService class should contain logic for downloading something from the internet and should write it into custom control am I right?

Edit 2 - That's the solution, but in my opinion control should be independable of ViewModel, am I right? ViewModel:

public ICommand ResetCommand {get; set;}

From UserControl's OnLoad method:

private void MyUserControl_Loaded(object sender, RoutedEventArgs e)
{
    MyUserControl ctrl = sender as MyUserControl;
    if (ctrl == null) return;

    MyViewModel vm = ctrl.DataContext as MyViewModel ;

    if (vm == null)
        return;

    vm.ResetCommand = new RelayCommand(param => this.Reset());
}

The WebBrwoserTask class doesn't open a specified URL in a web control defined in you app. It opens it in the phone's native web browser app.

You can launch the task from your ViewModel:

WebBrowserTask task = new WebBrowserTask();
task.Uri = new Uri("http://your-url-here");
task.Show();

I didn't get your question correctly but may be you want to show something from view model? You can do one thing pass the return value make a flag kinda thing in View Model and access it in View.

It depends on the framework you are using. You can either use a delegate, service or message to abstract away the task. You are not supposed to place it in the view nor you are supposed to call it directly from the viewmodel since that is not mockable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM