0

I am trying to learn Caliburn micro to use in Windows Phone 8 app development. But I am facing problem as it only works for main view model and is not able to connect any other view model to its view. Here is the code for other page than main page. Please suggest a solution to this problem. Thanks in advance

Page1.xaml

<phone:PhoneApplicationPage
    x:Class="CalibMicro.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBlock x:Name="TextBox2" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="50" />
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>

Page1ViewModel

using System.Windows;
namespace CalibMicro {
    public class Page1ViewModel : Caliburn.Micro.PropertyChangedBase 
    {
        public Page1ViewModel()
        {
            MessageBox.Show("Hello World !");
            TextBox2 = "Hello World";
        }

        private string _textBox2;
        public string TextBox2
        {
            get { return _textBox2; }
            set
            {
                _textBox2 = value;
                NotifyOfPropertyChange(() => TextBox2);
            }
        }
    }
}

AppBootStrapper

namespace CalibMicro {
    using System;
    using System.Collections.Generic;
    using System.Windows.Controls;
    using Microsoft.Phone.Controls;
    using Caliburn.Micro;

    public class AppBootstrapper : PhoneBootstrapperBase {
        PhoneContainer container;

        public AppBootstrapper() {
            Initialize();
        }

        protected override void Configure() {
            container = new PhoneContainer();
            if (!Execute.InDesignMode)
                container.RegisterPhoneServices(RootFrame);

            container.PerRequest<MainPageViewModel>();
            container.PerRequest<Page1ViewModel>();

            AddCustomConventions();
        }

        protected override object GetInstance(Type service, string key) {
            var instance = container.GetInstance(service, key);
            if (instance != null)
                return instance;

            throw new InvalidOperationException("Could not locate any instances.");
        }

        protected override IEnumerable<object> GetAllInstances(Type service) {
            return container.GetAllInstances(service);
        }

        protected override void BuildUp(object instance) {
            container.BuildUp(instance);
        }

        static void AddCustomConventions() {
            ConventionManager.AddElementConvention<Pivot>(Pivot.ItemsSourceProperty, "SelectedItem", "SelectionChanged").ApplyBinding =
                (viewModelType, path, property, element, convention) => {
                    if (ConventionManager
                        .GetElementConvention(typeof(ItemsControl))
                        .ApplyBinding(viewModelType, path, property, element, convention)) {
                        ConventionManager
                            .ConfigureSelectedItem(element, Pivot.SelectedItemProperty, viewModelType, path);
                        ConventionManager
                            .ApplyHeaderTemplate(element, Pivot.HeaderTemplateProperty, null, viewModelType);
                        return true;
                    }

                    return false;
                };

            ConventionManager.AddElementConvention<Panorama>(Panorama.ItemsSourceProperty, "SelectedItem", "SelectionChanged").ApplyBinding =
                (viewModelType, path, property, element, convention) => {
                    if (ConventionManager
                        .GetElementConvention(typeof(ItemsControl))
                        .ApplyBinding(viewModelType, path, property, element, convention)) {
                        ConventionManager
                            .ConfigureSelectedItem(element, Panorama.SelectedItemProperty, viewModelType, path);
                        ConventionManager
                            .ApplyHeaderTemplate(element, Panorama.HeaderTemplateProperty, null, viewModelType);
                        return true;
                    }

                    return false;
                };
        }
    }
}

2 Answers 2

1

CalibMicro.Page1 should be CalibMicro.Page1View since your viewmodel is named Page1ViewModel

0
0

Try to inherit Screen instead of PropertyChangedBase

Page1ViewModel:Screen
4
  • What do you mean by "is not able to connect any other view model to its view" ? Jun 30, 2014 at 8:04
  • @Radenko.. When I run the app page1.xaml is loading but the message box and textbox which is there in page1viewmodel is not i.e view is working but viewmodel is not.
    – Rohit Jain
    Jun 30, 2014 at 8:18
  • As I understand you are navigating from MainViewModel to Page1ViewModel and constructor of Page1ViewModel is not firing? Can you also give us code which navigate to Page1ViewModel? Jun 30, 2014 at 8:41
  • I have made initial navigation page to Page1 in AppManifest
    – Rohit Jain
    Jun 30, 2014 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.