簡體   English   中英

在頁面之間傳遞顏色

[英]Pass color between pages

我想將顏色從ColorPicker(Code4fun控件)傳遞到另一頁。

我通過這樣的參數:

 NavigationService.Navigate(new Uri("/GeneratePage.xaml?&foreground=" + qrCodeColorPicker.Color, UriKind.Relative)); 

我在生成器頁面上獲取價值存在問題。

var colorCode = NavigationContext.QueryString["foreground"];
Foreground = colorCode;

錯誤:

錯誤1無法將類型'string'隱式轉換為'System.Windows.Media.Color'

您知道我如何在第二頁上獲得價值嗎?

只是一個猜測,

var colorCode = "#FFDFD991";

Foreground = (Color)ColorConverter.ConvertFromString(colorCode);

您可以制作這樣的轉換器...

private Color ConvertHexStringToColour(string hexString)
{
  byte a = 0;
  byte r = 0;
  byte g = 0;
  byte b = 0;
  if (hexString.StartsWith("#"))
  {
    hexString = hexString.Substring(1, 8);
  }
  a = Convert.ToByte(Int32.Parse(hexString.Substring(0, 2), 
      System.Globalization.NumberStyles.AllowHexSpecifier));
  r = Convert.ToByte(Int32.Parse(hexString.Substring(2, 2), 
      System.Globalization.NumberStyles.AllowHexSpecifier));
  g = Convert.ToByte(Int32.Parse(hexString.Substring(4, 2), 
      System.Globalization.NumberStyles.AllowHexSpecifier));
  b = Convert.ToByte(Int32.Parse(hexString.Substring(6, 2), System.Globalization.NumberStyles.AllowHexSpecifier));
  return Color.FromArgb(a, r, g, b);
}

然后像

var colorCode =  ConvertHexStringToColour(NavigationContext.QueryString["foreground"]);

查詢返回一個string ,您不能只登錄一個color variable
所以,有兩種選擇

  1. 使用Color.FromArgb方法,並將其從您剛收到的stringsubstrings string傳遞給int轉換。
  2. 使用IsolatedStorageSettings並將其添加到此頁面上,然后在下一頁中使用它。
    就像Foreground = (Color)isoSettings["passedColor"];

如果沒有任何幫助,請嘗試此方法。

導航到另一頁之前,

PhoneApplicationService.Current.State["Foreground"] = qrCodeColorPicker.Color;

在下一頁的OnNavigateTo中,獲取顏色:

Color foregroundColor = (Color)PhoneApplicationService.Current.State["Forground"];

當然,請檢查null和全部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM