Xamarin формы Передача значений из страниц с навигацией - PullRequest
0 голосов
/ 19 марта 2020

Изначально я сделал navigation.popasync на второй странице обратно на главную страницу и передал значения внутри navigation.pushasync. Однако теперь я хочу go на третью страницу, а затем выполнить обновление отметки времени lblEndDT на третьей странице. Я хочу обновить lblEndDT на главной странице, но btnDone на третьей странице. Как передать значение с главной страницы на третью страницу?

Главная страница

public partial class MainPage : ContentPage
{
    public string mainpagevalue;
    int offlinecount = 0;
    int onlinecount = 0;
    public MainPage()
    {
        InitializeComponent();

    }

    private void btnOffline_Clicked(object sender, EventArgs e)
    {
        offlinecount++;

        //Navigation.PushAsync(new SecondPage(this, lblEndDT));

        Navigation.PushAsync(new OfflinePage(this, lblEndDT, btnOnline));


        if (offlinecount == 1)
        {
            string currentDT = DateTime.Now.ToString();
            lblStartDT.Text = currentDT;

        }


    }

    private void btnOnline_Clicked(object sender, EventArgs e)
    {
        onlinecount++;


        Navigation.PushAsync(new OnlinePage());


        if (onlinecount == 1)
        {
            string currentDT = DateTime.Now.ToString();
            lblStartDT.Text = currentDT;

        }
    }
  }
}

главная страница xaml

<Grid>
    <Grid.RowDefinitions>

        <RowDefinition Height="30"/>
        <RowDefinition Height="70"/>
        <RowDefinition Height="30"/>
        <RowDefinition Height="20"/>
        <RowDefinition Height="70"/>
        <RowDefinition Height="30"/>
        <RowDefinition Height="30"/>

        <RowDefinition Height="10"/>
        <RowDefinition Height="70"/>
        <RowDefinition Height="30"/>
        <RowDefinition Height="20"/>
        <RowDefinition Height="70"/>
        <RowDefinition Height="30"/>
        <RowDefinition Height="30"/>

        <RowDefinition Height="10"/>
        <RowDefinition Height="70"/>
        <RowDefinition Height="30"/>
        <RowDefinition Height="30"/>

        <RowDefinition Height="10"/>
        <RowDefinition Height="70"/>
        <RowDefinition Height="30"/>
        <RowDefinition Height="20"/>
        <RowDefinition Height="70"/>
        <RowDefinition Height="30"/>
        <RowDefinition Height="30"/>

        <RowDefinition Height="10"/>
        <RowDefinition Height="70"/>
        <RowDefinition Height="30"/>
        <RowDefinition Height="30"/>

        <RowDefinition Height="10"/>
        <RowDefinition Height="50"/>
        <RowDefinition Height="20"/>

    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="30"/>
        <ColumnDefinition Width="auto" />
        <ColumnDefinition Width="60" />
        <ColumnDefinition Width="auto"/>
        <ColumnDefinition Width="auto"/>
        <ColumnDefinition Width="70"/>
    </Grid.ColumnDefinitions>



    <!--OFFLINE TOOL-->
    <ImageButton x:Name="btnOffline" IsEnabled="True" Source="@drawable/offlinetool.png" Grid.Row="1" Grid.Column="1" BackgroundColor="Transparent" Clicked="btnOffline_Clicked"/>
    <Label Text="Offline Tool" Grid.Row="2" Grid.Column="1" Margin="15,0,0,0"/>

    <Label Text="Start Date Time:" Grid.Row="1" Grid.Column="3"/>
    <Label Text="End Date Time:" Grid.Row="1" Grid.Column="3" Margin="7,40,0,0"/>

    <Label x:Name="lblStartDT" Text="" Grid.Column="4" Grid.Row="1"/>
    <Label x:Name="lblEndDT" Text="" Grid.Column="4" Grid.Row="1" Margin="0,40,0,0"/>


    <!--ONLINE TOOL-->
    <ImageButton x:Name="btnOnline" Source="@drawable/ot.png" Grid.Row="8" Grid.Column="1" BackgroundColor="Transparent" IsEnabled="False" Clicked="btnOnline_Clicked"/>
    <Label Text="Online Tool" Grid.Row="9" Grid.Column="1" Margin="19,0,0,0"/>

    <Label Text="Start Date Time:" Grid.Row="8" Grid.Column="3" />
    <Label Text="End Date Time:" Grid.Row="8" Grid.Column="3" Margin="7,40,0,0"/>
    <Label Text="Status:" Grid.Row="9" Grid.Column="3" Margin="58,0,0,0" />

    <Label x:Name="lblOnlineStartDT" Text="" Grid.Column="4" Grid.Row="8"/>
    <Label x:Name="lblOnlineEndDT" Text="" Grid.Column="4" Grid.Row="8" Margin="0,40,0,0"/>
    <Label x:Name="txtOnlineStatus" Text="NOT STARTED" TextColor="Red" Grid.Column="4" Grid.Row="9"/>





</Grid>

Автономная страница

public partial class OfflinePage : ContentPage
{
    Label offlineEndDT;
    MainPage mainpage;
    ImageButton btnonline;
    public OfflinePage()
    {
        InitializeComponent();
    }
    public OfflinePage(MainPage mpage, Label endDT, ImageButton onlineimage)
    {
        InitializeComponent();
        mainpage = mpage;
        offlineEndDT = endDT;
        btnonline = onlineimage;

    }
    private void Button_Clicked(object sender, EventArgs e)
    {
        App.offlineDonecount++;

        if (App.offlineDonecount == 1)
        {
            string edt = DateTime.Now.ToString();
            offlineEndDT.Text = edt;
            mainpage.mainpagevalue = offlineEndDT.Text;


        }

        btnonline.Source = "onlinetool";
        btnonline.IsEnabled = true;

        Navigation.PopAsync();


    }
  }
}

Онлайн страница

public partial class OnlinePage : ContentPage
{
    public OnlinePage()
    {
        InitializeComponent();
    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        Navigation.PushAsync(new ThirdPage());
    }
  }
}

Третья страница

public partial class ThirdPage : ContentPage
{
    public ThirdPage()
    {
        InitializeComponent();
    }
    private void BtnDone_Clicked(object sender, EventArgs e)
    {

    }
  }
 }

1 Ответ

1 голос
/ 19 марта 2020

Обновлено:

Создайте модель TimeStamp с онлайн, автономным временем начала и окончания.

 public class TimeStamp
{
    public string OnlineStartTime { get; set; }
    public string OnlineEndTime { get; set; }
    public string OfflineStartTime { get; set; }
    public string OfflineEndTime { get; set; }
}

MainPage:

Xaml:

      <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="70" />
            <RowDefinition Height="30" />
            <RowDefinition Height="20" />
            <RowDefinition Height="70" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="10" />
            <RowDefinition Height="70" />
            <RowDefinition Height="30" />
            <RowDefinition Height="20" />
            <RowDefinition Height="70" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="10" />
            <RowDefinition Height="70" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="10" />
            <RowDefinition Height="70" />
            <RowDefinition Height="30" />
            <RowDefinition Height="20" />
            <RowDefinition Height="70" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="10" />
            <RowDefinition Height="70" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="10" />
            <RowDefinition Height="50" />
            <RowDefinition Height="20" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="30" />
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="60" />
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="70" />
        </Grid.ColumnDefinitions>

        <!--  OFFLINE TOOL  -->
        <ImageButton
            x:Name="btnOffline"
            Grid.Row="1"
            Grid.Column="1"
            BackgroundColor="Transparent"
            Clicked="btnOffline_Clicked"
            IsEnabled="True"
            Source="monkeyicon.png" />
        <Label
            Grid.Row="2"
            Grid.Column="1"
            Margin="15,0,0,0"
            Text="Offline Tool" />
        <Label
            Grid.Row="1"
            Grid.Column="3"
            Text="Start Date Time:" />
        <Label
            Grid.Row="1"
            Grid.Column="3"
            Margin="0,40,0,0"
            Text="End Date Time:" />

        <Label
            x:Name="lblStartDT"
            Grid.Row="1"
            Grid.RowSpan="2"
            Grid.Column="4"
            Grid.ColumnSpan="2"
            Text="{Binding OfflineStartTime}" />
        <Label
            x:Name="lblEndDT"
            Grid.Row="1"
            Grid.RowSpan="2"
            Grid.Column="4"
            Grid.ColumnSpan="2"
            Margin="0,40,0,0"
            Text="{Binding OfflineEndTime}" />


        <!--  ONLINE TOOL  -->
        <ImageButton
            x:Name="btnOnline"
            Grid.Row="8"
            Grid.Column="1"
            BackgroundColor="Transparent"
            Clicked="btnOnline_Clicked"
            IsEnabled="True"
            Source="star_small.png" />
        <Label
            Grid.Row="9"
            Grid.Column="1"
            Margin="19,0,0,0"
            Text="Online Tool" />

        <Label
            Grid.Row="8"
            Grid.Column="3"
            Text="Start Date Time:" />
        <Label
            Grid.Row="8"
            Grid.Column="3"
            Margin="7,40,0,0"
            Text="End Date Time:" />
        <Label
            Grid.Row="9"
            Grid.Column="3"
            Margin="58,0,0,0"
            Text="Status:" />

        <Label
            x:Name="lblOnlineStartDT"
            Grid.Row="8"
            Grid.RowSpan="2"
            Grid.Column="4"
            Grid.ColumnSpan="2"
            Text="{Binding OnlineStartTime}" />
        <Label
            x:Name="lblOnlineEndDT"
            Grid.Row="8"
            Grid.RowSpan="2"
            Grid.Column="4"
            Grid.ColumnSpan="2"
            Margin="0,40,0,0"
            Text="{Binding OnlineEndTime}" />
        <Label
            x:Name="txtOnlineStatus"
            Grid.Row="9"
            Grid.Column="4"
            Text="NOT STARTED"
            TextColor="Red" />
    </Grid>

Код:

public partial class MainPage : ContentPage
{
    public string mainpagevalue;
    int offlinecount = 0;
    int onlinecount = 0;
    public static TimeStamp timeStamp = new TimeStamp();
    public MainPage()
    {
        InitializeComponent();
    }
    private async void btnOnline_Clicked(object sender, EventArgs e)
    {
        onlinecount++;
        if (onlinecount == 1)
        {
            string currentDT = DateTime.Now.ToString();
            lblOnlineStartDT.Text = currentDT;
            timeStamp.OnlineStartTime = currentDT;
        }
        await Navigation.PushAsync(new OnlinePage());
    }
    private void btnOffline_Clicked(object sender, EventArgs e)
    {
        offlinecount++;
        //Navigation.PushAsync(new SecondPage(this, lblEndDT));
        Navigation.PushAsync(new OfflinePage(this, lblEndDT, btnOnline));
        if (offlinecount == 1)
        {
            string currentDT = DateTime.Now.ToString();
            lblStartDT.Text = currentDT;
            timeStamp.OfflineStartTime = currentDT;
        }
    }
}

OfflinePage:

 public partial class OfflinePage : ContentPage
{
    Label offlineEndDT;
    MainPage mainpage;
    ImageButton btnonline;
    public OfflinePage()
    {
        InitializeComponent();
    }
    public OfflinePage(MainPage mpage, Label endDT, ImageButton onlineimage)
    {
        InitializeComponent();
        mainpage = mpage;
        offlineEndDT = endDT;
        btnonline = onlineimage;
    }
    private void Button_Clicked(object sender, EventArgs e)
    {
        App.offlineDonecount++;

        if (App.offlineDonecount == 1)
        {
            string edt = DateTime.Now.ToString();
            offlineEndDT.Text = edt;
            mainpage.mainpagevalue = offlineEndDT.Text;
            MainPage.timeStamp.OfflineEndTime = edt;
        }         
        btnonline.IsEnabled = true;
        Navigation.PopAsync();
    }
}

OnlinePage:

  private void Button_Clicked(object sender, EventArgs e)
    {          
        Navigation.PushAsync(new ThirdPage());
    }

Третья страница:

private async void BtnDone_Clicked(object sender, EventArgs e)
    {
        MainPage.timeStamp.OnlineEndTime = DateTime.Now.ToString();
        MainPage mainPage = new MainPage();
        mainPage.BindingContext = MainPage.timeStamp;
        await Navigation.PushAsync(mainPage);
    }

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...