locked
How to refresh the Frame using a view model in C# RRS feed

  • Question

  • I have a Frame named ChildWindows and I am using MVVM pattern for my design , so earlier I was refreshing the frame in the button click handler but now I want to use command so that I can remove the button handler , so the question is how can I refresh my frame from the view model . Please help
    Wednesday, June 4, 2014 6:40 AM

Answers

  • the window.current is giving me error

    and I did it in handler using

    ChildWindows.Refresh();


    I guess you are developing a WPF application then?

    Since your view model doesn't have any reference to the view nor any element (like the Frame) in it, you need to inject it with an interface that defines methods to navigate between pages and/or refresh a Frame.

    You could also have your view (the window or user control) implement an interface that exposes the Frame element:

    public interface IMainView
     {
      Frame Frame { get;}
     }

     

    View:

    public partial class MainWindow : Window, IMainView
     {
      public Frame Frame
      {
       get { return frame; }
      }
    
      public MainWindow()
      {
       InitializeComponent();
       this.DataContext = new MainViewModel(this);
      }
     }
    

     
    <Frame x:Name="frame"></Frame>
    

    ...and then inject the view model with a loose reference to the view:

     

    View Model:

    public class MainViewModel
     {
      private IMainView _view;
      public MainViewModel(IMainView view)
      {
       _view = view;
      }
    
      public void SomeMethod()
      {
       _view.Frame.Refresh();
      }
     }
    

    You could then access the Frame element in the from the view model through the interface like in the SomeMethod() example code above.

    You could also use the event aggregator pattern to communicate between a view model and a view: http://blog.magnusmontin.net/2014/02/28/using-the-event-aggregator-pattern-to-communicate-between-view-models/

    • Marked as answer by KartikGupta Wednesday, June 4, 2014 1:12 PM
    Wednesday, June 4, 2014 10:56 AM

All replies

  • ((Frame)Window.Current.Content).Navigate(typeof(YourPage))

    Or you could create an INavigationService interface and inject the implementation at runtime: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/61b66a9a-3557-4b6b-ab47-50bb9d44f38b/how-to-navigate-to-next-page-from-viewmodel-of-mvvm-design-in-metro-application?forum=winappswithcsharp

    Wednesday, June 4, 2014 9:17 AM
  • I don't want it to navigate it to a new page or something I just want the frame to refresh itself

    Wednesday, June 4, 2014 9:34 AM
  • I don't want it to navigate it to a new page or something I just want the frame to refresh itself


    Yes, but you still get a reference to the Frame using ((Frame)Window.Current.Content). You can then call call any method you want on it...

    "earlier I was refreshing the frame in the button click handler". How did you do that? Just do the same on the Frame element returned from ((Frame)Window.Current.Content) in the view model.

    Wednesday, June 4, 2014 9:53 AM
  • the window.current is giving me error

    and I did it in handler using

    ChildWindows.Refresh();

    Wednesday, June 4, 2014 10:17 AM
  • the window.current is giving me error

    and I did it in handler using

    ChildWindows.Refresh();


    I guess you are developing a WPF application then?

    Since your view model doesn't have any reference to the view nor any element (like the Frame) in it, you need to inject it with an interface that defines methods to navigate between pages and/or refresh a Frame.

    You could also have your view (the window or user control) implement an interface that exposes the Frame element:

    public interface IMainView
     {
      Frame Frame { get;}
     }

     

    View:

    public partial class MainWindow : Window, IMainView
     {
      public Frame Frame
      {
       get { return frame; }
      }
    
      public MainWindow()
      {
       InitializeComponent();
       this.DataContext = new MainViewModel(this);
      }
     }
    

     
    <Frame x:Name="frame"></Frame>
    

    ...and then inject the view model with a loose reference to the view:

     

    View Model:

    public class MainViewModel
     {
      private IMainView _view;
      public MainViewModel(IMainView view)
      {
       _view = view;
      }
    
      public void SomeMethod()
      {
       _view.Frame.Refresh();
      }
     }
    

    You could then access the Frame element in the from the view model through the interface like in the SomeMethod() example code above.

    You could also use the event aggregator pattern to communicate between a view model and a view: http://blog.magnusmontin.net/2014/02/28/using-the-event-aggregator-pattern-to-communicate-between-view-models/

    • Marked as answer by KartikGupta Wednesday, June 4, 2014 1:12 PM
    Wednesday, June 4, 2014 10:56 AM
  • yes I am building an wpf app with MVVM model and thanks for showing this techinique , with this I can access any control I want to access :)

    Wednesday, June 4, 2014 1:14 PM
  • I had a doubt .. does this IMainView interface , break the MVVM pattern?

    Monday, June 9, 2014 7:12 AM
  • I had a doubt .. does this IMainView interface , break the MVVM pattern?


    It doesn't break the pattern as the view model has no strong reference to the view, it only knows about an interface.
    Monday, June 9, 2014 8:09 AM
  • I would use mvvm light or prism messenger.  No necessity to pass any reference around.

    Add some code into the view code behind which listens for the message and acts. Send that message from the viewmodel.

    Totally decoupled and no mocking required for automated testing.

    http://www.mvvmlight.net/ 

    http://stackoverflow.com/questions/16993918/mvvm-light-messenger-sending-and-registering-objects

    Monday, June 9, 2014 12:16 PM