C#,Xamarin.Forms。基本上我正试图从另一个模块访问对象的属性。我确实搜索过StackOverflow,但找不到类似这个问题的任何内容。
具体来说:我正在尝试确定页面内的视图是否可见。这是从与页面对象不同的全局模块访问的。
顶级对象是“MainPage”,其中的视图是timerAndClock,taktTime等。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:D6500"
xmlns:CustomViews="clr-namespace:D6500.Views"
x:Class="D6500.MainPage">
<Grid RowSpacing="0" ColumnSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<CustomViews:TimerAndClock x:Name="timerAndClock" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Grid.Row="0" PropertyChanged="TabbedPage_PropertyChanged" Opacity="1" />
<CustomViews:TaktTime x:Name="taktTime" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Grid.Row="0" IsVisible="False" PropertyChanged="TabbedPage_PropertyChanged" Opacity="0" />
<CustomViews:Day_Counter x:Name="day_Counter" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Grid.Row="0" IsVisible="False" PropertyChanged="TabbedPage_PropertyChanged" Opacity="0" />
<CustomViews:mainDisplayList x:Name="mainDisplayList" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Grid.Row="0" IsVisible="False" PropertyChanged="TabbedPage_PropertyChanged" Opacity="0" />
<CustomViews:BottomNavigationBar x:Name="BottomNavigationbar" Grid.Row="1" HorizontalOptions="FillAndExpand" VerticalOptions="End" />
</Grid>
</ContentPage>
我正在尝试从全局模块访问。这是要点:
namespace D6500
{
public static class Config
{
public static async void TimerGSS_Elapsed(object sender, ElapsedEventArgs e)
{
if(MainPage.timerAndClock.IsVisible)
{
//Do view specific stuff
}
}
}
}
我得到的错误是'MainPage.timerAndClock' is not accessible due to its protection level
。虽然我理解变量不公开,但我不知道如何改变它,因为它是在XAML for MainPage中创建的(见上文)。谢谢你们!
答案 0 :(得分:0)
创建公共帮助方法或属性
public bool IsTimerViewVisible {
get {
return timerAndClock.IsVisible;
}
}
答案 1 :(得分:0)
我没有弄清楚我的初始访问问题,但这是我如何解决问题。
我必须找到对MainPage实例的引用。以下是我从TaktTime内部引用MainPage的方法。
namespace D6500.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TaktTime : ContentView
private void OnAppearing()
{
....
//Start timer after we have finished reading and writing to the display
var NP = Application.Current.MainPage as NavigationPage;
var mp = NP.RootPage as D6500.MainPage;
mp.timerGSS.Start();
}
}