【问题标题】:UWP : Setting the Width and Height of controls as percentage valuesUWP:将控件的宽度和高度设置为百分比值
【发布时间】:2015-09-12 11:28:24
【问题描述】:

我正在尝试使用StackPanel 控件来显示一些概览选项。当MinWindowWidth 大于768px 时,我需要使用 20% 的窗口宽度进行此控制。当它在移动设备上运行时,我需要控件来填充屏幕的宽度。

我按照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 中不能使用百分比值吗?还是我做错了什么?

【问题讨论】:

  • 改用Grid
  • @JustinXL 我会试试的。我需要它来使用状态触发器
  • @JustinXL 你能把它作为答案发布吗?我已经切换到使用网格

标签: xaml uwp


【解决方案1】:

您需要确定您当前所在的平台,并相应地设置值。 这听起来像一个转换器。 由于您只有 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();
    }

【讨论】:

  • 为此使用转换器是一个非常丑陋的解决方案。 op 最终使用的网格要干净得多。
猜你喜欢
  • 2010-10-17
  • 1970-01-01
  • 2013-03-13
  • 2012-04-05
  • 2013-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-19
相关资源
最近更新 更多