2015-09-12 77 views
0

我試圖使用StackPanel控件來顯示一些概述選項。當MinWindowWidth大於768px時,我需要使用窗口寬度的20%進行此控件。當這在移動設備上運行時,我需要控件來填充屏幕的寬度。UWP:將控件的寬度和高度設置爲百分比值

我按照question中的建議嘗試嘗試設置百分比值。但是,我收到錯誤 - 「*字符串不能轉換爲長度」。

這裏是我的XAML -

 <StackPanel x:Name="OverviewStack"> 
      <RelativePanel> 
       <StackPanel Margin="10" x:Name="User" Orientation="Horizontal"> 
        <Ellipse Margin="5" Width="50" Height="50"> 
         <Ellipse.Fill> 
          <ImageBrush> 
           <ImageBrush.ImageSource> 
            <BitmapImage UriSource="Assets/insp.jpg" /> 
           </ImageBrush.ImageSource> 
          </ImageBrush> 
         </Ellipse.Fill> 
        </Ellipse> 
        <TextBlock Margin="5" VerticalAlignment="Center" Text="Ajay Kelkar" /> 
       </StackPanel> 
       <StackPanel Margin="10" x:Name="Options" Orientation="Vertical"> 
        <TextBlock Margin="5" Text="Sample text" /> 
       </StackPanel> 
      </RelativePanel> 
     </StackPanel> 

在UWP是一個百分比值不可能?或者我做錯了什麼?

+0

使用'Grid'代替。 –

+0

@JustinXL我會試試看。我需要那個與國家觸發器 –

+0

@JustinXL你可以張貼作爲答案?我已經切換到使用網格 –

回答

0

您需要確定您當前正在使用哪個平臺,並相應地設置該值。 這聽起來像一個轉換器。 由於您只有1個UWP項目,您需要在轉換器中檢查您所在的平臺。這樣的信息可以獲取這樣:

if (AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Mobile") { //Act accordingly }

所以我想你會喜歡做這樣的事情:

public object Convert(object value, Type targetType, object parameter, string language) 
    { 
     // bind the width as the value the converter is set upon 
     var width = double.Parse(value.ToString()); 
     if (AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Mobile") 
     { 
      return width; 
     } 

     // You weren't clear exactly regarding the 768px thing so act accordingly here 
     return width * 0.2d; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, string language) 
    { 
     throw new NotImplementedException(); 
    } 
+0

使用轉換器這是一個非常醜陋的解決方案。 最終使用的網格更清晰。 –

相關問題