/ uwp

Accumulated knowledge for develping UWP apps


This is an article for some knowledge involved with developing UWP apps, which contains some tips or tricks or lite solutions that help developing UWP apps.

Disable pinch and zoom in WebView

Unfortunately there is no way you can do with WebView control.

But by adding the following css code this can be implemented.

html, body

Dismiss soft keyboard

Assume that your keyboard is being focus on a TextBox named MyInputBox,the code:



can do this trick ;-D

Default style and ThemeResources

C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.10240.0\Generic

Binding in code-behind

Assume that you have a Page and a VM instance in Page. In addition, you define a DependencyProperty in this Page and want to bind it to the VM.

Here is the dp:

public bool ShowPhotoInfo
            get { return (bool)GetValue(ShowPhotoInfoProperty); }
            set { SetValue(ShowPhotoInfoProperty, value); }

        public static readonly DependencyProperty ShowPhotoInfoProperty =
            DependencyProperty.Register("ShowPhotoInfo", typeof(bool), typeof(PhotoDetailPage), new PropertyMetadata(false,
                ((sender, e) =>
                    var control = sender as PhotoDetailPage;

The binding in Page's constructor:

 var b = new Binding()
                Source = PhotoVM,
                Path = new PropertyPath("ShowPhotoInfo"),
            BindingOperations.SetBinding(this, ShowPhotoInfoProperty, b);

Where PhotoVM is a property in this Page.

Set attached property in a setter

Like this:

<Setter Target="ContentGrid.(Grid.Column)" value="1"/>
<Setter Target="ContentGrid.(RelativePanel.LeftOf)" value="RootGrid"/>

XAML theme resources reference


XAML theme resources


The usage of TaskCompletionSource

Sometimes in a method you need to await a value before the code goes next, but when the value is got is beyond your control. In this case, TaskCompletionSource is good to use.

If you have code Custom Control by yourself, you may know that when the method OnApplyTemplate ,which you can get the UIElement you definded in your Style, is called is unknown, and before you do sth. regarding to your elements defined in Style, you should make sure OnApplyTemplate is called.

In this case, first add this field:

public TaskCompletionSource<int> tcs;

then initial it:

tcs = new TaskCompletionSource<int>();

At the end of OnApplyTemplate , after getting the UIElement,set the value of tcs:


Assume that you have a method called Update() , before you do sth. to this.TitleTextBlock ,await tcs.Task:

private async Task Update()
    await tcs.Task;

If you call Update() before OnApplyTemplate is done,it will await OnApplyTemplate and once the tcs 's task value is set the Update() method can continue.

Preserve the ending white-spaces in TextBlock

If you just need to display white-spaces in TextBlock in your xaml:

  <Run Text="Example&#160;&#160;&#160;"/>

The XAML above will display a TextBlock control and it shows "Example   ".Note that the code above use the escaped characters &#160; to do the trick.

If you need to generate the TextBlock in Code-behind, you need to replace white spaces with (char)160 :


<TextBlock x:Name="textBlock" />

Code-behind:Assume that the input texts are from a TextBox, register a TextChanged handler and:

private void tb_TextChanged(object sender, TextChangedEventArgs e)
    var text= (sender as TextBox).Text;
    text=text.Replace(' ', (char)160);
    textBlock.Inlines.Add(new Run()