1
votes

I have a WP7application app containing a xaml file called page1.xaml with a viewmodel . The xaml file contains a button with a binding command

I have another project called sampleapplication in which i am launching an emulator and have to display the above page1.xaml file which is in another project.

I am able to load the above xaml file from wp7 app in the current project using

(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/WP7application ;component/Views/page1.xaml", UriKind.Relative));

but i am unable to worked with the events after the xaml is loaded. How can i get the button worked in my current project ?

i have added all the references to the wp7 app view and viewmodels in my current sampleapplication

2
Do you have any binding errors in the Output window? How bout adding a short but complete sample that shows the exact problem?Blachshma

2 Answers

0
votes

You can easily have the view and view model in separate assemblies but the assembly (app or library) which has the view in it must have a reference to the library that the view-model is in.

There are two things to be aware of though:
1. If you're using different assemblies for the view and view-model the view-model must be in a class library and not the main application.
2. Be sure to structure your code so that you don't have any circular references. (This can require discipline to avoid as complexity grows.)

0
votes

It sounds as though the DataContext of your view is not set to an instance of the view model. There are several ways you can do this.

The easiest is simply to put the following code in the Loaded event of the view:

private void PhoneApplicationPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
    DataContext = new ViewModel();
}

The preferred way is to define a view model locator in your application project. Create an instance of your view model.

public class ViewModelLocator
{
    private readonly ViewModel _viewModel = new ViewModel();

    public ViewModel Main
    {
        get { return _viewModel; }
    }
}

Create your view model locator in the App.XAML:

<Application xmlns:vm="clr-namespace:groovd.client.phone.ViewModels" >
    <Application.Resources>
        <ResourceDictionary>
            <vm:ViewModelLocator xmlns:vm="clr-namespace:MyApp.ViewModels" x:Key="Locator" />
        </ResourceDictionary>
    </Application.Resources>
</Application>

Then get the property from the view model locator in the page:

<phone:PhoneApplicationPage 
    DataContext="{Binding Main, Source={StaticResource Locator}}">
</phone:PhoneApplicationPage>