1839

How to get a reference to window from a user control using Caliburn Micro

Question:

I want to get a reference from a user control view model to the window that holds the user control. How can I do this?

If I can get a reference to the view from view model then I can use:

Window parentWindow = Window.GetWindow(userControlReference);

So my questions are:

<ol><li>What's the best way to get the reference from user control view model to the window that holds the user control?</li> <li>If I want to use the above code, what is the best way to get a reference to view from view model in Caliburn Micro?</li> </ol>

Answer1:

A viewmodel will generally inherit from IViewAware assuming that it's an IScreen implementation

You can just cast a Screen to IViewAware and use the GetView() method to get a reference to the view. I think you can implement IViewAware on your own viewmodels and Caliburn will automatically raise the ViewAttached event so you can keep a reference to the view but I might have a look at the docs

Bottom line: check out the IViewAware interface

Edit:

From the docs...

IViewAware – Implemented by classes which need to be made aware of the view that they are bound to. It has an AttachView method which is called by the framework when it binds the view to the instance. It has a GetView method which the framework calls before creating a view for the instance. This enables caching of complex views or even complex view resolution logic. Finally, it has an event which should be raised when a view is attached to the instance called ViewAttached.

Recommend

  • Concurrent modification during iteration while trying to remove object from a list
  • Open a jstree in a jquery dialog box
  • Android X: tools:replace specified at line: for attribute, but no new value specified
  • Video with transparency on Android
  • Building with Lombok's @Slf4j and Eclipse: Cannot find symbol log
  • Can you block a website from being in a browser's history?
  • Issue in open my application from browser url in android
  • Sapui5: How can I set an initial sort order in smarttable?
  • Is MulticastSocket supported in Android 2.3?
  • Condition on a timestamp column to select data for a year
  • AngularJS User Login to Json file
  • Saving data from textboxes into text file
  • How to get the device Model in objective c?
  • Macbook React Native install
  • Viewport for ipad portrait [only]
  • java string index out of bound exception
  • Converting a wstring to jstring on Linux
  • How to install or uninstall SonarQube plug-ins with HTTP?
  • How to add a progress ring to the splash screen in Windows 8?
  • Reference after posting data from web worker
  • Downgrade NG2 Directive to AngularJS
  • how to ignore files when finishing private ClearCase branch?
  • pass sessionid through jquery ajax call to php
  • Mock request/post with mockito
  • How to detect beginning of line, or: “The name 'getCharPositionInLine' does not exist in t
  • Insert inline image into Lotus Notes message
  • OpenCV::solvePNP() - Assertion failed
  • Bulk loading into PostgreSQL from a remote client
  • Android NDK refer to external libraries in JNI
  • Remove stopwords and tolower function slow on a Corpus in R
  • Write to elasticsearch from spark is very slow
  • Multiple canvases (pages) in Fabric.js
  • Find all parks for a given zipcode with google maps
  • content must have a ListView whose id attribute is 'android.R.id.list'
  • Autocomplete source from project settings
  • How to specify generic type when the type is only known at runtime?
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • Android Library Projects on Windows and Mac