【问题标题】:Ellipse stretch has no effect椭圆拉伸没有效果
【发布时间】:2019-01-12 11:45:57
【问题描述】:

谁能解释为什么StretchEllipse 元素中无效。无论我使用None,Fill,UniformToFill 还是Uniform,结果都是一样的。

如果我在 DataTemplate (GridView) 中使用相同的代码,它可以正常工作。

<Button x:Name="UI_Application_LogIn_ProfilePictureButton_Button" Style="{StaticResource LogInButton}" BorderBrush="{ThemeResource SystemControlForegroundBaseMediumBrush}" IsTabStop="False" UseSystemFocusVisuals="False">
    <Grid x:Name="UI_Application_LogIn_ProfilePicture_Grid" IsHitTestVisible="False">
        <Ellipse x:Name="UI_Application_LogIn_ProfilePicture" Width="160" Height="160">
            <Ellipse.Fill>
                <ImageBrush x:Name="UI_Application_LogIn_ProfilePictureImageBrush" Stretch="UniformToFill" AlignmentY="Top"/>
            </Ellipse.Fill>
        </Ellipse>
        <Ellipse x:Name="UI_Application_LogIn_ProfilePictureNonStaticLightEffect" Width="160" Height="160" Fill="{ThemeResource SystemControlHighlightTransparentRevealBorderBrush}"/>
    </Grid>
</Button>

【问题讨论】:

  • 我找到了拉伸不起作用的原因。真是脑残。当我创建新的ImageBrush 时,我忘记在我的代码中使用Stretch。所以在创建新的ImageBrush 后添加行ShapeImageBrush.Stretch = Stretch.UniformToFill; 解决问题。

标签: c# uwp uwp-xaml ellipse stretch


【解决方案1】:

如果要创建圆形图像,请使用 windows 社区工具包中的 Imagex 控件

您可以查找并使用 imagex here 或者您可以在 Microsoft.Toolkit.Uwp.UI.Controls 上通过 nuget 引用您的应用 至于您当前的问题,这是因为拉伸仅影响日食内部的画笔而不影响日食本身,为了进行比例调整,您必须将日食包裹在 Viewbox 周围控件,然后以与图像相同的方式设置其 Stretch 属性

【讨论】:

    【解决方案2】:

    我找到了拉伸不起作用的原因。真是脑残。创建新 ImageBrush 时,我忘记在代码中使用 Stretch。因此,在创建新的 ImageBrush 后添加行 if (BrushStretch != null) (TargetShape.Fill as ImageBrush).Stretch = BrushStretch.Value; 可以解决问题。

        //STORAGE FILE TO SHAPE
        public static async Task<Shape> StorageFileToShape(Shape TargetShape, StorageFile SourceStorageFile, Stretch? BrushStretch, AlignmentX? BrushAlignmentX, AlignmentY? BrushAlignmentY)
        {
            //IF SHAPE NULL RETURN NULL
            if (TargetShape == null) return null;
            //IF STORAGEFILE NULL OR NOT AVAILABLE RETURN NULL
            if (SourceStorageFile == null || !SourceStorageFile.IsAvailable) return null;
            //IF BRUSH IS NULL OR ITS TYPE ISN'T 'ImageBrush' CREATE NEW BRUSH
            if (TargetShape.Fill == null || TargetShape.Fill.GetType() != typeof(ImageBrush)) TargetShape.Fill = new ImageBrush();
            //SET STRETCH
            if (BrushStretch != null) (TargetShape.Fill as ImageBrush).Stretch = BrushStretch.Value;
            //SET ALIGNMENT X
            if (BrushAlignmentX != null) (TargetShape.Fill as ImageBrush).AlignmentX = BrushAlignmentX.Value;
            //SET ALIGNMENT Y
            if (BrushAlignmentY != null) (TargetShape.Fill as ImageBrush).AlignmentY = BrushAlignmentY.Value;
            //GET PICTURE 
            (TargetShape.Fill as ImageBrush).ImageSource = await StorageFileToBitmapImage(SourceStorageFile);
            //SET SHAPE FILL
            TargetShape.Fill = TargetShape.Fill as ImageBrush;
            //RETURN SHAPE
            return TargetShape;
        }
        //STORAGE FILE TO BITMAP IMAGE
        public static async Task<BitmapImage> StorageFileToBitmapImage(StorageFile SourceStorageFile)
        {
            BitmapImage TargetBitmapImage = new BitmapImage();
            TargetBitmapImage.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
            using (var BitmapImageFileStream = await SourceStorageFile.OpenAsync(FileAccessMode.Read))
            {
                await TargetBitmapImage.SetSourceAsync(BitmapImageFileStream);
            }
            return TargetBitmapImage;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-20
      • 2016-09-13
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多