从OnLaunched()完全访问MainPage控件 [英] Full Access of MainPage Controls from OnLaunched()

查看:56
本文介绍了从OnLaunched()完全访问MainPage控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经与UWP进行了一段时间的斗争,我似乎无法找到一种专业的方式来从OnLaunched()访问MainPage控件。 

I have fought with UWP for a while and I cannot seem to find a professional way to have access of the MainPage controls from OnLaunched(). 

 

我希望能够与VB.NET中的form.Load()和form.Shown()具有相同的访问权限

I want to be able to have the same access to everything as with form.Load() and form.Shown() in VB.NET 

 

我想要做的一件事是检查应用程序的本地设置,并安排控件,如TextBlocks,TextBoxes,Buttons,ComboBoxes,MenuItems,SubmenuItems, etcetera,基于本地设置的值。 

One of the things I want to do is to check the local settings of the application and to arrange controls such as TextBlocks, TextBoxes, Buttons, ComboBoxes, MenuItems, SubmenuItems, etcetera, based on the values of the local settings. 

 

哪种方式最好,最专业的方式呢?  

WHICH IS THE BEST AND THE MOST PROFESSIONAL WAY TO DO SO? 

 

我更喜欢Visual Basic示例,但我会采取任何措施。 

I prefer a Visual Basic examples but I would take anything. 

 

谢谢。 

 

一个肮脏的方法是使用组合框。组合框在初始化时调用。 ComboBox配置中的Try Catch更改子例程将捕获NullReferenceException。被抓住时,CoboBox在初始化时被更改。在
内部捕获一个定时器可以实例化或一个新线程。该线程将等待在OnLaunch中激活后设置的全局变量。但是看起来不是很专业。 

One dirty way around is to use a ComboBox. ComboBoxes are called at initialisation. A Try Catch in the ComboBox configuration changed subroutine would catch the NullReferenceException. When caught, the CoboBox was changed at initialisation. Inside the catch a timer can be instantiated or a new thread. This thread would wait for a global variable which is set after the Activate in OnLaunch. Does not look very professional, though. 

 

另一种可能性是以编程方式提出一个事件,比如来自OnLaunch的警报()并以我想要的方式初始化应用程序。也不是很专业。 

Another possibility may be to programmatically raise an event, say, an alarm from OnLaunched() and to initialize the application the way I want from within. Also not very professional. 

 

经过一些试验和错误以及我在这个论坛上看过这篇文章之后: 

After some trials and errors and after I have read this post on this forum : 

 

https://social.msdn.microsoft.com/Forums/windowsapps/en-US/21f5dc32-d79e-4427-b3b7 -a61543c8fa47 / access-mainpage-from-appxaml-?forum = winappswithcsharp 

https://social.msdn.microsoft.com/Forums/windowsapps/en-US/21f5dc32-d79e-4427-b3b7-a61543c8fa47/accessing-mainpage-from-appxaml-?forum=winappswithcsharp 

 

我想出了一些现在有用的东西,我不知道多久: 

I came up with something which works for now, I do not know for how long : 

 

转到app.xaml.vb,注释掉自动生成的受保护的覆盖Sub OnLaunched(args作为LaunchActivatedEventArgs)并改为执行此简化: 

Go to app.xaml.vb, comment out the automatically generated Protected Overrides Sub OnLaunched(args As LaunchActivatedEventArgs) and do this simplified sub instead : 

 

受保护的覆盖Sub OnLaunched(args为LaunchActivatedEventArgs)

Protected Overrides Sub OnLaunched(args As LaunchActivatedEventArgs)

        Dim thePage As MainPage = New MainPage

        Dim thePage As MainPage = New MainPage

        Window.Current.Content = thePage

        Window.Current.Content = thePage

        Window.Current.Activate()

        Window.Current.Activate()

        thePage.SomePublicSubWhichIsDeclaredAndDefinedInMainPageClass()

        thePage.SomePublicSubWhichIsDeclaredAndDefinedInMainPageClass()

End Sub 

End Sub 

 

然后,转到MainPage.xaml .vb并在MainPage类中写入子: 

Then, go to MainPage.xaml.vb and write the sub inside of MainPage class : 

 

Public NotInheritable Class MainPage

  ;  继承页面

Public NotInheritable Class MainPage
    Inherits Page

'随时随地

Public Sub SomePublicSubWhichIsDeclaredAndDefinedInMainPageClass ()

Public Sub SomePublicSubWhichIsDeclaredAndDefinedInMainPageClass()

SomeTextBlockDefinedInMainPageDotXaml.Text =" Garbage"

SomeTextBlockDefinedInMainPageDotXaml.Text="Garbage"

End Sub

'随时随地

结束等级

似乎工作。 

Seem to work. 

 

我认为,诀窍是定位从主页调用sub的代码: 
thePage.SomePublicSubWhichIsDeclaredAndDefinedInMainPageClass()在激活之后而不是之前。最有可能的是,主页面将使用MainPage.xaml的默认值激活,然后可以更改这些默认值,就像我将TextBlock  SomeTextBlockDefinedInMainPageDotXaml.Text更改为
一样垃圾。这看起来像form.Shown(),在表单被加载并显示之后发生的事件。我没有尝试过,但很有可能,当sub被称为BEFORE Activate时,
表单可能会在加载默认值之前更改,类似于form.Load()可能会生成异常。我整晚都没有睡觉,因为愚蠢而感到厌倦和厌倦,所以我没有尝试过,只有在激活之后,这可能是因为我对
的使用,但是: 

I think, the trick is to positioned the code which calls the sub from the main page :  thePage.SomePublicSubWhichIsDeclaredAndDefinedInMainPageClass() AFTER THE ACTIVATE AND NOT BEFORE. Most likely, the main page will be activated with the defaults from MainPage.xaml and then these defaults can be changed the same way I changed the TextBlock SomeTextBlockDefinedInMainPageDotXaml.Text to Garbage. This looks a lot as form.Shown(), an event which happens AFTER the form is LOADED and shown. I have not tried but, most likely, when the sub is called BEFORE Activate, the form may be changed before the default is loaded similar to form.Load() OR exceptions may be generated. I have not slept the whole night and am sick and tired with stupidity, so I have not tried BEFORE only AFTER Activate which is probably what I am to use but : 

 

我会寻求更好的解决方案,以及任何建议,太过分了。 

I DO LOOK FOR BETTER SOLUTIONS AS WELL AS FOR ANY SUGGESTIONS, WORSE TOO. 

 

我没有尝试从标准自动生成的子OnLaunched中调用MainPage中的sub,但是当代码在If或之前的Sub Sub中激活之后应该工作。  ;

I have not tried to put the call to the sub in MainPage from within the standardly and automatically generated sub OnLaunched but should work when the code is after Activated in the If or before End Sub. 

 

我不确定并且我大量使用的条件句的任何澄清可能,本来应该等等非常受欢迎。 

Any clarifications for the conditional sentences which I am not sure of and I have heavily used may, would, should, etcetera are very welcome too. 

 

推荐答案

这是一个有趣的问题。 在开发使用XAML的应用程序时,许多开发人员已经转而使用MVVM模式。 使用此模式,您不会直接与控件绑定不同属性的控件进行交互。
视图模型中属性的控件。 例如,您可以使用IValueconverter将布尔值更改为可见性。  
This is an interesting question.  A lot of developers have switched to using the MVVM pattern when developing applications that use XAML.  Using this pattern you do not directly interact with the controls you bind the different properties of the controls to properties in your view model.  You can use use an IValueconverter to change a boolean to a visibility for example.  


这篇关于从OnLaunched()完全访问MainPage控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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