Windows phone 8 用于Windows Phone 8的Caliburn Micro不工作
我正在尝试学习Caliburn micro,以便在Windows Phone 8应用程序开发中使用。但我面临的问题是,它只适用于主视图模型,无法将任何其他视图模型连接到其视图。这是主页以外的其他页面的代码。请提出解决这个问题的办法。提前感谢 Page1.xamlWindows phone 8 用于Windows Phone 8的Caliburn Micro不工作,windows-phone-8,caliburn.micro,Windows Phone 8,Caliburn.micro,我正在尝试学习Caliburn micro,以便在Windows Phone 8应用程序开发中使用。但我面临的问题是,它只适用于主视图模型,无法将任何其他视图模型连接到其视图。这是主页以外的其他页面的代码。请提出解决这个问题的办法。提前感谢 Page1.xaml <phone:PhoneApplicationPage x:Class="CalibMicro.Page1" xmlns="http://schemas.microsoft.com/winfx/2006/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>
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;
};
}
}
}
namespace CalibMicro{
使用制度;
使用System.Collections.Generic;
使用System.Windows.Controls;
使用Microsoft.Phone.Controls;
使用Caliburn.Micro;
公共类AppBootstrapper:PhoneBootstrapperBase{
电话容器;
公共AppBootstrapper(){
初始化();
}
受保护的覆盖无效配置(){
container=newphonecontainer();
如果(!Execute.InDesignMode)
容器注册器电话服务(根框架);
container.PerRequest();
container.PerRequest();
AddCustomConventions();
}
受保护的覆盖对象GetInstance(类型服务,字符串键){
var instance=container.GetInstance(服务,键);
if(实例!=null)
返回实例;
抛出新的InvalidOperationException(“找不到任何实例”);
}
受保护的重写IEnumerable GetAllInstances(类型服务){
返回容器。GetAllInstances(服务);
}
受保护的覆盖空洞堆积(对象实例){
容器。堆积(实例);
}
静态void AddCustomConventions(){
ConventionManager.AddElementConvention(Pivot.ItemsSourceProperty,“SelectedItem”,“SelectionChanged”).ApplyBinding=
(viewModelType、路径、属性、元素、约定)=>{
国际单项体育联合会(会议经理)
.GetElementConvention(typeof(ItemsControl))
.ApplyBinding(viewModelType、路径、属性、元素、约定)){
会议经理
.ConfigureSelectedItem(元素,透视。SelectedItemProperty,viewModelType,路径);
会议经理
.ApplyHeaderTemplate(元素,Pivot.HeaderTemplateProperty,null,viewModelType);
返回true;
}
返回false;
};
ConventionManager.AddElementConvention(Panorama.ItemsSourceProperty,“SelectedItem”,“SelectionChanged”).ApplyBinding=
(viewModelType、路径、属性、元素、约定)=>{
国际单项体育联合会(会议经理)
.GetElementConvention(typeof(ItemsControl))
.ApplyBinding(viewModelType、路径、属性、元素、约定)){
会议经理
.ConfigureSelectedItem(元素、全景、SelectedItemProperty、viewModelType、路径);
会议经理
.ApplyHeaderTemplate(元素,Panorama.HeaderTemplateProperty,null,viewModelType);
返回true;
}
返回false;
};
}
}
}
尝试继承Screen而不是PropertyChangedBase
Page1ViewModel:Screen
CalibMicro.Page1应该是CalibMicro.Page1View,因为您的视图模型名为Page1ViewModel您所说的“无法将任何其他视图模型连接到其视图”是什么意思?@Radenko。。运行应用程序时,page1.xaml正在加载,但page1viewmodel中的消息框和文本框不工作,即view正在工作,但viewmodel不工作。据我所知,您正在从MainViewModel导航到page1viewmodel,并且page1viewmodel的构造函数没有启动?您还可以给我们导航到Page1ViewModel的代码吗?我已经在AppManifest中创建了导航到Page1的初始页面