应用程序栏在MainPage类上运行方法 [英] App Bar Running Methods on the MainPage class

查看:80
本文介绍了应用程序栏在MainPage类上运行方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我刚开始为Windows Phone开发,我正在尝试构建一个底部有多个不同应用程序栏配置的Pivot应用程序(某些页面上的不同按钮)。我按照Microsoft指南通过在App.xaml中定义
不同的ApplicationBars并通过在SelectionChanged事件中的MainPage.xaml.cs中交换它们来获取此行为。根据这里的指南:


msdn.microsoft.com/en-us/library/windowsphone/develop/hh394036%28v=vs.105%29.aspx


(MSDN不允许我发布链接,因为我的帐户现在未经验证...)


但是,我想要按钮在App.xaml和App.xaml.cs中定义,以执行属于MainPage类的非静态方法。这就是毛茸茸的地方。


现在看来,似乎没有简单/安全/推荐的方法来实现这一目标。我使用了一些现在正常工作的代码。下面的代码详细介绍了我用来从App.xaml.cs调用MainPage类上的方法的方法。它在类本身中创建MainPage类的静态实例
。这似乎不太安全。


这是代码:

使用Microsoft.Phone.Controls;使用System.Windows 
;

命名空间PhoneApp1
{
公共部分类MainPage:PhoneApplicationPage
{
公共静态MainPage实例
{
get;
私人套装;
}

public MainPage()
{
InitializeComponent();
instance = this;
}

public bool DoSomeWorkInMainPage()
{
bool result = true;

//做点什么

返回结果;
}
}
}

所有这些都说,我只是有几个问题:


如果一个Pivot应用程序有多个不同的应用程序栏取决于页面,而按钮能够在MainPage类上调用方法,那么如何实现这种行为?


和两个:App.xaml / App.xaml.cs文件的确切内容是什么?你应该在那里写什么样的代码?


我不想采取可能影响我的应用程序稳定性的不良做法。 Microsoft建议在数据透视应用程序中交换应用程序栏的方法似乎是最优雅的解决方案,但它似乎不适用于我的程序结构的其余部分。


我可以重组吗?我的程序,以便App.xaml.cs包含我想要执行的代码(它几乎访问传感器)?或者不建议将主应用程序代码放在应用程序代码文件中?


感谢您的帮助。

解决方案

您可以在MainPage中编写代码

< phone:PhoneApplicationPage 
x:Class =" WindowsPhoneApp.MainPage"
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"
mc:Ignorable =" d"
FontFamily =" {StaticResource PhoneFontFamilyNormal}"
FontSize =" {StaticResource PhoneFontSizeNormal}"
Foreground =" Black"
SupportedOrientations =" Portrait"取向= QUOT;肖像"
shell:SystemTray.IsVisible =" True">
< phone:PhoneApplicationPage.Resources>
<! - 您的代码 - >
< / phone:PhoneApplicationPage.Resources>

或者您可以参考以下链接


http://amitchandnz.wordpress.com/2012/01/31/using-different-应用条换枢轴页/


Hello,

I've just started to develop for Windows Phone and I'm trying to build a Pivot app that has multiple different Application Bar configurations on the bottom (different buttons on some pages). I followed the Microsoft guide to obtaining this behavior by defining different ApplicationBars in the App.xaml and by swapping through them in the MainPage.xaml.cs in the SelectionChanged event. As per the guide here:

msdn.microsoft.com/en-us/library/windowsphone/develop/hh394036%28v=vs.105%29.aspx

(MSDN won't let me post links because my account is unverified right now...)

However, I want the buttons defined in App.xaml and App.xaml.cs to execute non-static methods that are part of the MainPage class. This is where it gets hairy.

As it stands now, there appears to be no simple/safe/recommended way to accomplish this. I used a bit of code that works right now. The code below details the method I used to call methods on the MainPage class from App.xaml.cs. It creates a static instance of the MainPage class in the class itself. This doesn't seem very safe.

Here is the code:

using Microsoft.Phone.Controls;
using System.Windows;

namespace PhoneApp1
{
	public partial class MainPage: PhoneApplicationPage
	{
		public static MainPage instance
		{
		   get;
		   private set;
		}

		public MainPage()
		{
		   InitializeComponent();
		   instance = this;
		}

		public bool DoSomeWorkInMainPage()
		{
		   bool result = true;

		   //do something

		   return result;
		}
	}
}

All this being said, I just have a few questions:

How would one accomplish the kind of behavior where a Pivot app has multiple different app bars depending on the page, while the buttons are able to call methods on the MainPage class?

And two: What exactly is the App.xaml/App.xaml.cs file for? What kind of code are you supposed to write in there?

I don't want to pick up bad practices that can compromise the stability of my apps. The method Microsoft recommends for swapping App bars in a pivot app seems to be the most elegant solution, but it doesn't seem to work with the rest of my program's structure.

Could I restructure my program so that the App.xaml.cs contains the code I want to execute (it pretty much accesses sensors)? Or is it not recommended to put main application code in the App code file?

Thanks for all your help.

解决方案

Instead of having in App.xaml you can write the code in MainPage

<phone:PhoneApplicationPage
    x:Class="WindowsPhoneApp.MainPage"
    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"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="Black" 
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">
    <phone:PhoneApplicationPage.Resources>
        <!--Your code here-->
    </phone:PhoneApplicationPage.Resources>

Or you can refer following link

http://amitchandnz.wordpress.com/2012/01/31/using-different-application-bars-for-pivot-pages/


这篇关于应用程序栏在MainPage类上运行方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆