2011-11-23 79 views
1

我試圖以模擬遺憾的是沒有在Windows 7手機存在一個Android UI元素:ListPreference透明畫布,用不透明的元素

我想過使用一個彈出,將採取完全整個屏幕(模擬一個模態窗口)。

所以彈出將作出以下元素組成:

彈出 - >帆布 - >邊框 - >的StackPanel - >單選按鈕

畫布將是完全透明的(或輕微發白清楚地表明下面的元素不可用)

該邊框將被製作爲僅包含所有RadioButton的大小 然後,StackPanel將是不透明和黑色的。

不幸的是,如果我使底部畫布透明,所有子元素都是透明的。我只能使的元素更透明

透明度的工作方式與Android或iPhone(在父級完全透明但不透明的子級中很容易)略有不同。

有沒有辦法使父母完全透明,孩子不透明?

或者,也許有人可以建議另一種方式來模擬模態窗口。 誰知道,也許有人甚至開發了一個ListPreference樣的UIElement :)

謝謝

+0

你介意發佈您的XAML代碼? –

回答

2

這是我最終做到的。

它的工作方式與Android上的ListPreference類似。構造函數的字符串,字符串數組和指示一個int這是默認值

當窗戶關閉時,駁回被稱爲委託..

所以你怎麼稱呼它,像這樣:

string[] choices = { "Choice 1", "Choice 2", "Choice3" }; 
ListPreference lp = new ListPreference("name", choices, 1); 
lp.dismissed += new ListPreferences.DismissedHandler(lp_Dismissed); 

代碼:

public class ListPreference 
{ 
    Popup p; 
    string Name; 
    int oldValue; 

    public delegate void DismissedHandler(string name, bool changed, int newvalue); 

    public event DismissedHandler Dismissed; 

    public bool IsOpen 
    { 
     get 
     { 
      return p.IsOpen; 
     } 

     set 
     { 
      p.IsOpen = value; 
     } 
    } 

    public ListPreference(string name, Array elements, int default_value) 
    { 
     p = new Popup(); 
     Name = name; 
     Dismissed = null; 
     oldValue = default_value; 

     double height = (App.Current.RootVisual as FrameworkElement).ActualHeight; 
     double width = (App.Current.RootVisual as FrameworkElement).ActualWidth; 

     p.VerticalOffset = SystemTray.IsVisible ? 32.0 : 0.0; 
     p.Height = height; 
     p.Width = width; 
     Canvas canvas = new Canvas(); 
     SolidColorBrush colorBrush = new SolidColorBrush(Colors.Black); 
     colorBrush.Opacity = 0.75; 
     //Color.FromArgb(0xff, 0x8a, 0x8a, 0x8a)); 
     canvas.Background = colorBrush; 
     //canvas.Opacity = 0.765; 
     canvas.Height = height; 
     canvas.Width = width; 
     p.Child = canvas; 

     Border border = new Border(); 
     border.Width = width - 50.0 * 2.0; 
     border.BorderBrush = new SolidColorBrush(Colors.LightGray); 
     border.BorderThickness = new Thickness(5.0); 
     border.Background = new SolidColorBrush(Colors.Black); 
     canvas.Children.Add(border); 

     StackPanel panel2 = new StackPanel(); 
     panel2.Orientation = System.Windows.Controls.Orientation.Vertical; 

     int i = 0; 
     foreach (string val in elements) 
     { 
      RadioButton radio1 = new RadioButton(); 
      radio1.GroupName = "group1"; 
      radio1.Content = val; 
      if (i == default_value) 
       radio1.IsChecked = true; 
      int j = i; 
      radio1.Click += (sender, args) => radio1_Checked(radio1, j); 
      i++; 
      panel2.Children.Add(radio1); 
     } 

     Button button1 = new Button(); 
     button1.Background = new SolidColorBrush(Colors.Black); 
     button1.Foreground = new SolidColorBrush(Colors.White); 
     button1.Opacity = 1.0; 
     button1.Content = "Cancel"; 
     button1.Margin = new Thickness(5.0); 
     button1.Click += new RoutedEventHandler(closeButton_Click); 
     panel2.Children.Add(button1); 
     border.Child = panel2; 

     // Open the popup. 
     p.IsOpen = true; 
     p.UpdateLayout(); 
     border.Height = panel2.DesiredSize.Height + 5.0 * 2.0; 
     border.SetValue(Canvas.TopProperty, (height - border.Height)/2.0); 
     border.SetValue(Canvas.LeftProperty, (width - border.Width)/2.0); 
     p.UpdateLayout(); 
    } 

    void closeButton_Click(object sender, RoutedEventArgs e) 
    { 
     // Close the popup. 
     p.IsOpen = false; 
     if (Dismissed != null) 
     { 
      Dismissed(Name, false, -1); 
     } 
    } 

    void radio1_Checked(object sender, int idx) 
    { 
     p.IsOpen = false; 
     if (Dismissed != null) 
     { 
      Dismissed(Name, idx != oldValue, idx); 
     } 
    } 
} 
1

我建議建立一個Usercontrol,會做你的需要。將LayoutRoot網格的background設置爲PhoneSemitransparentBrush或更改opacity也會更改子元素的不透明度。那麼你的孩子元素可以有任何你想要的不透明度。您可以將此控件作爲子項添加到彈出窗口中。此外,您可以將doubleanimation添加到彈出式窗口中,其中包含openedclosed事件觸發器。將UserControl的設計高度更改爲480x760以模擬整頁。

回答你的問題。將類似PhoneSemitransparentBrushTransparentBrush的資源用於Canvas背景是您的選擇之一。 Opacity將改變包括其子女在內的整個UIElement的不透明度。