2016-04-14 16 views
0

Windowsフォームと同様に、複数のパネルを同じフォームに追加することができ、特定の条件に基づいて特定のパネルを表示および非表示にすることができます。このように使用できるXamarin.Formsには何らかのコントロールがありますか?Xamarin.formsのパネル機能を実装する方法

これの主な理由は、ページに3つのタブがあることです。タブ付きのバーのデザインは私のクライアントが望むものではないため、タブ用のボタンとタブ付きのページは使用していません。だから私は3つのページがPage APage BPage Cとそれぞれのページにそれぞれのページに行く3つのタブがあると言う。ユーザーがPage Aにいて、まだ保存されていないデータを入力してPage Bに行きたい場合はPage Bに入力し、Page Bにデータを保存せずにPage Aに戻ると、詳細が入力されます。 Page APage Bにユーザーが入力してください。

したがって、複数のページを使用すると、新しいページにリダイレクトするとデータが失われます。だから私は複数のパネルを持っていて、第2パネルが見えるときに第1パネルを隠す方法はありますか?これはデータをクリアしないので、私は自分が望むものを達成することができます。

答えて

1

IsVisibleプロパティを使用して)使用されていないパネルを非表示にするだけで、ビジュアルツリーからそれらを取り出しても、メモリから解放することはありません。

ページごとにコンテンツビューを作成すると、この例のようにメインUIで簡単に使用できます。

MyView.cs(これはあなたがあなたのパネルにしたい何でもかまいません)::

using System; 
using Xamarin.Forms; 

namespace FormsSandbox 
{ 
    public class MyView : ContentView 
    { 
     public static BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(MyView), 
      String.Empty, BindingMode.Default, null, TextChanged); 

     public string Text { 
      get { 
       return (string)GetValue (TextProperty); 
      } 
      set { 
       SetValue (TextProperty, value); 
      } 
     } 

     private Label _contentLabel; 

     public MyView() 
     { 
      _contentLabel = new Label { 
       FontSize = 56, 
       FontAttributes = FontAttributes.Bold, 
       HorizontalTextAlignment = TextAlignment.Center, 
       VerticalTextAlignment = TextAlignment.Center 
      }; 
      Content = _contentLabel; 
     } 

     static void TextChanged (BindableObject bindable, object oldValue, object newValue) 
     { 
      var view = (MyView)bindable; 
      view._contentLabel.Text = (newValue ?? "").ToString(); 
     } 
    } 
} 

XamlPage.xaml(メインUIのページに、私たちは隠されている間、彼らはまだメモリに残る個々のパネルを隠しているにもかかわらず):

<?xml version="1.0" encoding="UTF-8"?> 
<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:sandbox="clr-namespace:FormsSandbox" 
    x:Class="FormsSandbox.XamlPage"> 
    <ContentPage.Padding> 
     <OnPlatform x:TypeArguments="Thickness" iOS="0,20,0,0" Android="0" WinPhone="0"/> 
    </ContentPage.Padding> 

    <Grid RowSpacing="0" ColumnSpacing="0"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*" /> 
      <ColumnDefinition Width="*" /> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 

     <Button Text="1" Clicked="ButtonClicked" x:Name="Button1" Grid.Column="0" /> 
     <Button Text="2" Clicked="ButtonClicked" x:Name="Button2" Grid.Column="1" /> 
     <Button Text="3" Clicked="ButtonClicked" x:Name="Button3" Grid.Column="2" /> 

     <sandbox:MyView Text="1" x:Name="View1" Grid.Row="1" Grid.ColumnSpan="3" /> 
     <sandbox:MyView Text="2" x:Name="View2" Grid.Row="1" Grid.ColumnSpan="3" /> 
     <sandbox:MyView Text="3" x:Name="View3" Grid.Row="1" Grid.ColumnSpan="3" /> 
    </Grid> 

</ContentPage> 

XamlPage.xaml.cs:

using System; 
using Xamarin.Forms; 

namespace FormsSandbox 
{ 
    public partial class XamlPage : ContentPage 
    { 
     public XamlPage() 
     { 
      InitializeComponent(); 
      SelectButton (Button1); 
     } 

     void SelectButton(Button button) 
     { 
      View view = null; 
      if (button == Button1) 
       view = View1; 
      if (button == Button2) 
       view = View2; 
      if (button == Button3) 
       view = View3; 
      View1.IsVisible = View1 == view; 
      View2.IsVisible = View2 == view; 
      View3.IsVisible = View3 == view; 
      Button1.TextColor = (Button1 == button) ? Color.Accent.AddLuminosity(0.18) : (Color)Button.TextColorProperty.DefaultValue; 
      Button2.TextColor = (Button2 == button) ? Color.Accent.AddLuminosity(0.18) : (Color)Button.TextColorProperty.DefaultValue; 
      Button3.TextColor = (Button3 == button) ? Color.Accent.AddLuminosity(0.18) : (Color)Button.TextColorProperty.DefaultValue; 
      Button1.BackgroundColor = (Button1 == button) ? Color.Silver.AddLuminosity(0.18) : Color.Silver.AddLuminosity(0.1); 
      Button2.BackgroundColor = (Button2 == button) ? Color.Silver.AddLuminosity(0.18) : Color.Silver.AddLuminosity(0.1); 
      Button3.BackgroundColor = (Button3 == button) ? Color.Silver.AddLuminosity(0.18) : Color.Silver.AddLuminosity(0.1); 
     } 

     void ButtonClicked (object sender, EventArgs e) 
     { 
      SelectButton ((Button)sender); 
     } 
    } 
} 

Running example

+0

ありがとうございました。 – Arti

+0

[質問](http://stackoverflow.com/questions/36641270/access-controls-from-view-in-content-page)をご覧ください。私はcontentPageのcontentviewエントリフィールドにアクセスできません – Arti

+0

私もその質問を見て、役立つ答えを追加しました。それはあなたのために働いている場合、他の人があなたがもはや立ち往生していない問題を解決しようと時間を費やさないようにマークしてください。 –

1

このデータをキャッシュに保存し、そこからロードすることができます。

public static class MyDataCache 
{ 
    public static MyData MyData { get; } = new MyData(); 
} 

// in your pages 
protected override void OnAppearing() 
{    
    base.OnAppearing(); 
    // set data from MyDataCache.MyData 
    MyProperty = MyDataCache.MyData.MyProperty; 
} 

protected override void OnDisappearing() 
{    
    base.OnDisappearing(); 
    // set data to MyDataCache.MyData 
    MyDataCache.MyData.MyProperty = MyProperty; 
} 

ただし、それは単なるメモリキャッシュです。アプリケーションがトゥーンストーンになった場合、データは失われます。このアプローチを最初に試して、そのニーズに合っているかどうかを確認することができます。その後、データを一時的なストレージに保存する必要があります(例:Akavache)。このページのナビゲーション動作をカスタムで再構築しないでください。

+0

キャッシュに保存することで、値はページ変更時でも維持されますか? – Arti

+0

はい。その静的クラスです。アプリが記憶されている限り、その記憶にあります。ページが表示されたら、プロパティを入力するだけです。 –

+0

ちょうど確認するには、ページの読み込み時に 'ページA'私はXMLファイルからデータを読み込み、それをシリアライズしてテキストボックスや他のコントロールに表示します。ユーザーが'ページB 'に行くと、データが消えてデータが保存されます。静的変数。それで私が 'Page B'から' Page A 'に戻ると、私は静的変数の保存されたデータをコントロールに追加しなければなりません。 – Arti

関連する問題