简体   繁体   English

如何以编程方式放置按钮-UWP

[英]How to position a button programmatically - UWP

I want to programmatically place a button at bottom right of my screen. 我想以编程方式在屏幕的右下角放置一个按钮。 Button should place at 25% of height and width of screen(on bottom right).How can I done it programmatically? 按钮应放置在屏幕高度和宽度的25%处(在右下角)。如何以编程方式完成操作?

var displayInformation = DisplayInformation.GetForCurrentView();
ScreenHeight= (int)displayInformation.ScreenHeightInRawPixels;//1080
ScreenWidth= (int)displayInformation.ScreenWidthInRawPixels;//1920

  <Button x:Name="btnTest"   VerticalAlignment="Bottom">

According to your comment, you want the distance from the bottom/right corner. 根据您的评论,您需要距下/右角的距离。 You can achive that, by setting the margin of the button and setting the alignments. 您可以通过设置按钮的边距并设置对齐方式来实现。
To make the margin depending the application/page size you can use the following snippet: 要使边距取决于应用程序/页面大小,可以使用以下代码段:

public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            this.SizeChanged += MainPage_SizeChanged;
        }

        private void MainPage_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            var height = this.ActualHeight;
            var width = this.ActualWidth;

            btnTest.Margin = new Thickness(0, 0, 0.25 * width, 0.25 * height);
        }
    }

You can either set the alignment in the MainPage_SizeChanged : 您可以在MainPage_SizeChanged设置对齐方式:

btnTest.VerticalAlignment = VerticalAlignment.Bottom;
btnTest.HorizontalAlignment = HorizontalAlignment.Right;

Another option is to set the alignment in the xaml: 另一个选择是在xaml中设置对齐方式:

<Button x:Name="btnTest" Content="Test" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM