Xamarin访问视图可从另一个模块可视化



c#,xamarin.forms。基本上,我正在尝试从另一个模块访问对象的属性。我确实搜索了stackoverflow,但找不到类似的特定问题。

特别是:我正在尝试确定页面内部是否可见。这是从与页面对象不同的全局模块访问的。

顶级对象是"主页",其中的视图是计时器,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中为主页创建的(请参见上文(。谢谢大家!

创建公共助手方法或属性

public bool IsTimerViewVisible {
  get {
    return timerAndClock.IsVisible;
  }
}

我没有弄清楚我的初始访问问题,但这是我解决问题的方式。

  1. 我将函数timergss_elapsed((从config模块移至mainpage,它是本地上下文。
  2. 该功能不再是静态的,它在子视图中打破了参考。
  3. 我必须找到对主页实例的引用。以下是我从内部到主页的引用的方式。

    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();
        }
    }
    

最新更新