Merge pull request #136 from dotnet/master

Manual publish fixing bad squash-merge
This commit is contained in:
Andy De George
2020-11-24 10:34:28 -08:00
committed by GitHub
11 changed files with 197 additions and 64 deletions
@@ -1,5 +1,6 @@
---
title: "Defining Default Values with the ShouldSerialize and Reset Methods"
description: "Learn how to use the ShouldSerialize and Reset property methods to control the Windows Forms designer behavior."
ms.date: "03/30/2017"
dev_langs:
- "csharp"
@@ -10,7 +11,7 @@ helpviewer_keywords:
ms.assetid: 7b6c5e00-3771-46b4-9142-5a80d5864a5e
---
# Defining Default Values with the ShouldSerialize and Reset Methods
`ShouldSerialize` and `Reset` are optional methods that you can provide for a property, if the property does not a have simple default value. If the property has a simple default value, you should apply the <xref:System.ComponentModel.DefaultValueAttribute> and supply the default value to the attribute class constructor instead. Either of these mechanisms enables the following features in the designer:
`ShouldSerialize` and `Reset` are optional methods that you can provide for a property, if the property does not have a simple default value. If the property has a simple default value, you should apply the <xref:System.ComponentModel.DefaultValueAttribute> and supply the default value to the attribute class constructor instead. Either of these mechanisms enables the following features in the designer:
- The property provides visual indication in the property browser if it has been modified from its default value.
@@ -60,6 +61,9 @@ private bool ShouldSerializeMyFont()
}
```
> [!TIP]
> If you want to permanently prevent a property from being serialized by the designer, add the [DesignerSerializationVisibility](xref:System.ComponentModel.DesignerSerializationVisibilityAttribute) attribute with the value of `Hidden`.
A complete code example follows.
```vb
@@ -147,3 +151,4 @@ public class MyControl : Control {
- [Properties in Windows Forms Controls](properties-in-windows-forms-controls.md)
- [Defining a Property](defining-a-property-in-windows-forms-controls.md)
- [Property-Changed Events](property-changed-events.md)
- <xref:System.ComponentModel.DesignerSerializationVisibilityAttribute?displayProperty=fullName>
@@ -66,8 +66,6 @@ In addition to creating events using the Windows Forms Designer in Visual Studio
button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
```
The <xref:System.ComponentModel.EventHandlerList.AddHandler%2A> method demonstrated in the Visual Basic code above establishes a click event handler for the button.
## See also
- [Creating Event Handlers in Windows Forms](creating-event-handlers-in-windows-forms.md)
@@ -1,11 +1,11 @@
### YamlMime:Landing
title: .NET Desktop Guide for Windows Forms
summary: Learn about using Windows Forms on Windows with either .NET Framework, .NET 5 and above, or .NET Core 3.1.
summary: Learn about Windows Forms (WinForms), a graphical user interface for Windows and .NET Framework.
metadata:
title: Windows Forms for .NET documentation
description: Learn about using Windows Forms (WinForms), a graphical user interface for Windows and .NET.
description: Learn about Windows Forms (WinForms), a graphical user interface for Windows and .NET Framework.
ms.topic: landing-page
ms.date: 08/30/2020
@@ -27,7 +27,7 @@ landingContent:
url: /visualstudio/designers/walkthrough-windows-forms-designer
- text: Create a WinForms app from the command-line
url: how-to-create-a-windows-forms-application-from-the-command-line.md
- title: Controls
linkLists:
- linkListType: overview
@@ -59,4 +59,28 @@ landingContent:
links:
- text: Order in which events are raised
url: order-of-events-in-windows-forms.md
- title: Input
linkLists:
- linkListType: overview
links:
- text: About keyboard input
url: how-keyboard-input-works.md
- text: About mouse input
url: how-mouse-input-works-in-windows-forms.md
- linkListType: concept
links:
- text: Keyboard events
url: using-keyboard-events.md
- text: Mouse events
url: mouse-events-in-windows-forms.md
- text: Mouse pointers
url: mouse-pointers-in-windows-forms.md
- linkListType: how-to-guide
links:
- text: Modify keyboard input
url: how-to-modify-keyboard-input-to-a-standard-control.md
- text: Detect modifier keyboard keys
url: how-to-determine-which-modifier-key-was-pressed.md
- text: Distinguish between single/double clicks
url: how-to-distinguish-between-clicks-and-double-clicks.md
@@ -11,21 +11,23 @@ helpviewer_keywords:
ms.assetid: a57776d1-f353-4794-afa0-bfa3c712ed1c
---
# How to: Use Special Characters in XAML
Markup files that are created in Visual Studio are automatically saved in the Unicode UTF-8 file format, which means that most special characters, such as accent marks, are encoded correctly. However, there is a set of commonly-used special characters that are handled differently. These special characters follow the World Wide Web Consortium (W3C) XML standard for encoding.
The following table shows the syntax for encoding this set of special characters:
|Character|Syntax|Description|
|---------------|------------|-----------------|
|<|`&lt;`|Less than symbol.|
|>|`&gt;`|Greater than sign.|
|&|`&amp;`|Ampersand symbol.|
|"|`&quot;`|Double quote symbol.|
Markup files that are created in Visual Studio are automatically saved in the Unicode UTF-8 file format, which means that most special characters, such as accent marks, are encoded correctly. However, there is a set of commonly-used special characters that are handled differently. These special characters follow the World [Wide Web Consortium (W3C) XML standard for encoding](https://www.w3resource.com/xml/reserved-markup-characters.php).
The following table shows the syntax for encoding this set of special characters:
| Character | Syntax | Description |
|-----------|----------|----------------------|
| `<` | `&lt;` | Less than symbol. |
| `>` | `&gt;` | Greater than sign. |
| `&` | `&amp;` | Ampersand symbol. |
| `"` | `&quot;` | Double quote symbol. |
| `'` | `&apos;` | Single quote symbol. |
> [!NOTE]
> If you create a markup file using a text editor, such as Windows Notepad, you must save the file in the Unicode UTF-8 file format in order to preserve any encoded special characters.
The following example shows how you can use special characters in text when creating markup.
## Example
[!code-xaml[SpecialCharsSnippets#SpecialCharsSnippet1](~/samples/snippets/csharp/VS_Snippets_Wpf/SpecialCharsSnippets/CS/Window1.xaml#specialcharssnippet1)]
> If you create a markup file using a text editor, such as Windows Notepad, you must save the file in the Unicode UTF-8 file format in order to preserve any encoded special characters.
The following example shows how you can use special characters in text when creating markup.
## Example
[!code-xaml[SpecialCharsSnippets#SpecialCharsSnippet1](~/samples/snippets/csharp/VS_Snippets_Wpf/SpecialCharsSnippets/CS/Window1.xaml#specialcharssnippet1)]
@@ -1,6 +1,6 @@
---
title: "Storyboards Overview"
desription: Organize and apply animations in storyboards. Use property-targeting syntax and combine timelines in Windows Presentation Foundation (WPF).
description: Organize and apply animations in storyboards. Use property-targeting syntax and combine timelines in Windows Presentation Foundation (WPF).
ms.date: "03/30/2017"
dev_langs:
- "csharp"
@@ -65,7 +65,9 @@ The following table shows the different places where each <xref:System.Windows.
|--------------------------------|-------------------|-----------|----------------------|-------------------|-------------|
|<xref:System.Windows.Media.Animation.BeginStoryboard> and an <xref:System.Windows.EventTrigger>|Yes|Yes|Yes|Yes|[Animate a Property by Using a Storyboard](how-to-animate-a-property-by-using-a-storyboard.md)|
|<xref:System.Windows.Media.Animation.BeginStoryboard> and a property <xref:System.Windows.Trigger>|No|Yes|Yes|Yes|[Trigger an Animation When a Property Value Changes](how-to-trigger-an-animation-when-a-property-value-changes.md)|
|<xref:System.Windows.Media.Animation.BeginStoryboard> and a property <xref:System.Windows.MultiTrigger>|No|Yes|Yes|Yes|[MultiTrigger class example](/dotnet/api/system.windows.multitrigger#examples)|
|<xref:System.Windows.Media.Animation.BeginStoryboard> and a <xref:System.Windows.DataTrigger>|No|Yes|Yes|Yes|[How to: Trigger an Animation When Data Changes](/previous-versions/dotnet/netframework-3.5/aa970679(v=vs.90))|
|<xref:System.Windows.Media.Animation.BeginStoryboard> and a <xref:System.Windows.MultiDataTrigger>|No|Yes|Yes|Yes|[MultiDataTrigger class example](/dotnet/api/system.windows.multidatatrigger#examples)|
|<xref:System.Windows.Media.Animation.Storyboard.Begin%2A> method|Yes|No|No|No|[Animate a Property by Using a Storyboard](how-to-animate-a-property-by-using-a-storyboard.md)|
The following example uses a <xref:System.Windows.Media.Animation.Storyboard> to animate the <xref:System.Windows.FrameworkElement.Width%2A> of a <xref:System.Windows.Shapes.Rectangle> element and the <xref:System.Windows.Media.SolidColorBrush.Color%2A> of a <xref:System.Windows.Media.SolidColorBrush> used to paint that <xref:System.Windows.Shapes.Rectangle>.
@@ -80,7 +82,10 @@ The following sections describe the <xref:System.Windows.Media.Animation.Storybo
## Targeting Framework Elements, Framework Content Elements, and Freezables
The previous section mentioned that, for an animation to find its target, it must know the target's name and the property to animate. Specifying the property to animate is straight forward: simply set <xref:System.Windows.Media.Animation.Storyboard.TargetProperty?displayProperty=nameWithType> with the name of the property to animate. You specify the name of the object whose property you want to animate by setting the <xref:System.Windows.Media.Animation.Storyboard.TargetName%2A?displayProperty=nameWithType> property on the animation.
The previous section mentioned that, for an animation to find its target, it must know the target's name and the property to animate. Specifying the property to animate is straight forward: simply set `TargetProperty` with the name of the property to animate. You specify the name of the object whose property you want to animate by setting the <xref:System.Windows.Media.Animation.Storyboard.TargetName%2A?displayProperty=nameWithType> property on the animation.
> [!CAUTION]
> While you can use the `Target` property to bind directly to an object as an alternative to `TargetName`, it isn't serializable. There is no guaranteed that the `Target` object can be correctly referenced in XAML.
For the <xref:System.Windows.Setter.TargetName%2A> property to work, the targeted object must have a name. Assigning a name to a <xref:System.Windows.FrameworkElement> or a <xref:System.Windows.FrameworkContentElement> in [!INCLUDE[TLA2#tla_xaml](../../../includes/tla2sharptla-xaml-md.md)] is different than assigning a name to a <xref:System.Windows.Freezable> object.
@@ -88,22 +88,26 @@ With the _Form1_ form designer open, use the **Toolbox** pane to add the followi
You can position and size the controls according to the following settings. Either visually move them to match the screenshot that follows, or click on each control and configure the settings in the **Properties** pane. You can also click on the form title area to select the form:
| Object | Setting | Value |
|---------|----------|------------|
| Form | Text | `Names` |
| | Size | `268, 180` |
| Label | Location | `12, 9` |
| | Text | `Names` |
| Listbox | Name | `lstNames` |
| | Location | `12, 27` |
| | Size | `120, 94` |
| Textbox | Name | `txtName` |
| | Location | `138, 26` |
| | Size | `100, 23` |
| Button | Name | `btnAdd` |
| | Location | `138, 55` |
| | Size | `100, 23` |
| | Text | `Add Name` |
| Object | Setting | Value |
|-------------|----------|------------|
| **Form** | Text | `Names` |
| | Size | `268, 180` |
| | | |
| **Label** | Location | `12, 9` |
| | Text | `Names` |
| | | |
| **Listbox** | Name | `lstNames` |
| | Location | `12, 27` |
| | Size | `120, 94` |
| | | |
| **Textbox** | Name | `txtName` |
| | Location | `138, 26` |
| | Size | `100, 23` |
| | | |
| **Button** | Name | `btnAdd` |
| | Location | `138, 55` |
| | Size | `100, 23` |
| | Text | `Add Name` |
You should have a form in the designer that looks similar to the following:
@@ -49,16 +49,16 @@ When migrating a .NET Framework Windows Forms application, there are a few thing
## Back up your projects
The first step to migrating a project is to back up your project! If something goes wrong, you can restore your code to its original state by restoring your backup. Don't rely on tools such as the .NET Portability Analyzer to back up your project, even if they seem to. It's better to have a copy of the original project safely stored in the cloud or elsewhere on your computer.
The first step to migrating a project is to back up your project! If something goes wrong, you can restore your code to its original state by restoring your backup. Don't rely on tools such as the .NET Portability Analyzer to back up your project, even if they seem to. It's best to personally create a copy of the original project.
## NuGet packages
If your project is referencing NuGet packages, you probably have a **packages.config** file in your project folder. With SDK-style projects, NuGet package references are configured in the project file. Visual Studio project files can optionally define NuGet packages in the project file too. .NET 5 doesn't use **packages.config** for NuGet packages. NuGet package references must be migrated into the project file before migration.
To migrate the **packages.config** file, do the following:
To migrate the **packages.config** file, do the following steps:
01. In **Solution explorer**, find the project you're migrating.
02. Right-click on **packages.config** > **Migrate packages.config to ProjectReference**.
02. Right-click on **packages.config** > **Migrate packages.config to PackageReference**.
03. Select all of the top-level packages.
A build report is generated to let you know of any issues migrating the NuGet packages.
@@ -67,13 +67,15 @@ A build report is generated to let you know of any issues migrating the NuGet pa
The next step in migrating your app is converting the project file. As previously stated, .NET 5 uses SDK-style project files and won't load the Visual Studio project files that .NET Framework uses. However, there's the possibility that you're already using SDK-style projects. You can easily spot the difference in Visual Studio. Right-click on the project file in **Solution explorer** and look for the **Edit Project File** menu option. If this menu item is missing, you're using the old Visual Studio project format and need to upgrade.
To upgrade, do the following:
Convert each project in your solution. If you're using the sample app previously referenced, both the **MatchingGame** and **MatchingGame.Logic** projects would be converted.
To convert a project, do the following steps:
01. In **Solution explorer**, find the project you're migrating.
01. Right-click on the project and select **Unload Project**.
01. Right-click on the project and select **Edit Project File**.
01. Copy-and-paste the project XML into a text editor. You'll want a copy so that it's easy to move content into the new project.
01. Erase the content of the file and paste in the following content:
01. Erase the content of the file and paste the following XML:
```xml
<Project Sdk="Microsoft.NET.Sdk">
@@ -91,14 +93,14 @@ To upgrade, do the following:
> [!IMPORTANT]
> Libraries don't need to define an `<OutputType>` setting. Remove that entry if you're upgrading a library project.
This XML gives you the basic structure of the project. However, it doesn't contain any of the settings from the old project file. Using the old project information you previously copied to a text editor, do the following:
This XML gives you the basic structure of the project. However, it doesn't contain any of the settings from the old project file. Using the old project information you previously copied to a text editor, do the following steps:
01. Copy the following elements from the old project file into the `<PropertyGroup>` element in the new project file:
- `<RootNamespace>`
- `<AssemblyName>`
Your project file should look similar to the following:
Your project file should look similar to the following XML:
```xml
<Project Sdk="Microsoft.NET.Sdk">
@@ -118,7 +120,7 @@ This XML gives you the basic structure of the project. However, it doesn't conta
01. Copy the `<ItemGroup>` elements from the old project file that contain `<ProjectReference>` or `<PackageReference>` into the new file after the `</PropertyGroup>` closing tag.
Your project file should look similar to the following:
Your project file should look similar to the following XML:
```xml
<Project Sdk="Microsoft.NET.Sdk">
@@ -142,7 +144,7 @@ This XML gives you the basic structure of the project. However, it doesn't conta
</Project>
```
The `<ProjectReference>` elements don't need the `<Project>` and `<Name>` children, so you can remove those:
The `<ProjectReference>` elements don't need the `<Project>` and `<Name>` children, so you can remove those settings:
```xml
<ItemGroup>
@@ -156,7 +158,7 @@ Windows Forms projects for .NET Framework typically include other files such as
Copy those entries from the old project file into an `<ItemGroup>` element in the new project. After you copy the entries, change any `<Compile Include="value">` or `<EmbeddedResource Include="value">` elements to instead use `Update` instead of `Include`.
- Import the configuration for the *Settings.settings* file. Note that `Include` was changed to `Update` on the `<Compile>` element:
- Import the configuration for the *Settings.settings* file. Notice that the `Include` was changed to `Update` on the `<Compile>` element:
```xml
<ItemGroup>
@@ -172,7 +174,10 @@ Copy those entries from the old project file into an `<ItemGroup>` element in th
</ItemGroup>
```
- Import the configuration for any *resx* file, such as the *properties/Resources.resx* file. Note that `Include` was changed to `Update` on both the `<Compile>` and `<EmbeddedResource>` elements, and `<SubType>` was removed from `<EmbeddedResource>`:
> [!IMPORTANT]
> **Visual Basic** projects typically use the folder *My Project* while C# projects typically use the folder *Properties* for the default project settings file.
- Import the configuration for any *resx* file, such as the *properties/Resources.resx* file. Notice that the `Include` was changed to `Update` on both the `<Compile>` and `<EmbeddedResource>` elements, and `<SubType>` was removed from `<EmbeddedResource>`:
```xml
<ItemGroup>
@@ -188,11 +193,97 @@ Copy those entries from the old project file into an `<ItemGroup>` element in th
</ItemGroup>
```
Convert each project in your solution. If you're using the sample app previously referenced, the **MatchingGame.Logic** project would be converted.
> [!IMPORTANT]
> **Visual Basic** projects typically use the folder *My Project* while C# projects typically use the folder *Properties* for the default project resource file.
### Visual Basic
Visual Basic language projects require extra configuration.
01. Import the configuration file *My Project\Application.myapp* setting. Notice that the `<None>` and `<Compile>` elements use the `Update` attribute instead of the `Include` attribute.
```xml
<ItemGroup>
<None Update="My Project\Application.myapp">
<Generator>MyApplicationCodeGenerator</Generator>
<LastGenOutput>Application.Designer.vb</LastGenOutput>
</None>
<Compile Update="My Project\Application.Designer.vb">
<AutoGen>True</AutoGen>
<DependentUpon>Application.myapp</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
</ItemGroup>
```
01. Add the `<MyType>WindowsForms</MyType>` setting to the `<PropertyGroup>` element:
```xml
<PropertyGroup>
(contains settings previously described)
<MyType>WindowsForms</MyType>
</PropertyGroup>
```
This setting imports the `My` namespace members Visual Basic programmers are familiar with.
01. Import the namespaces defined by your project.
Visual Basic projects can automatically import namespaces into every code file. Copy the `<ItemGroup>` elements from the old project file that contain `<Import>` into the new file after the `</PropertyGroup>` closing tag.
```xml
<ItemGroup>
<Import Include="Microsoft.VisualBasic" />
<Import Include="System" />
<Import Include="System.Collections" />
<Import Include="System.Collections.Generic" />
<Import Include="System.Data" />
<Import Include="System.Drawing" />
<Import Include="System.Diagnostics" />
<Import Include="System.Windows.Forms" />
<Import Include="System.Linq" />
<Import Include="System.Xml.Linq" />
<Import Include="System.Threading.Tasks" />
</ItemGroup>
```
If you can't find any `<Import>` statements, or your project fails to compile, make sure you at least have the following `<Import>` statements defined in your project:
```xml
<ItemGroup>
<Import Include="System.Data" />
<Import Include="System.Drawing" />
<Import Include="System.Windows.Forms" />
</ItemGroup>
```
01. From the original project, copy the `<Option*>` and `<StartupObject>` settings to the `<PropertyGroup>` element:
```xml
<PropertyGroup>
(contains settings previously described)
<OptionExplicit>On</OptionExplicit>
<OptionCompare>Binary</OptionCompare>
<OptionStrict>Off</OptionStrict>
<OptionInfer>On</OptionInfer>
<StartupObject>MatchingGame.My.MyApplication</StartupObject>
</PropertyGroup>
```
### Reload the project
After you convert a project to the new SDK-style format, reload the project in Visual Studio:
01. In **Solution Explorer**, find the project you converted.
01. Right-click on the project and select **Reload Project**.
If the project fails to load, you may have introduced a mistake in the XML of the project. Open the project file for editing and try to identify and fix the mistake. If you can't find a mistake, try starting over.
## Edit App.config
If your app has an *App.config* file, remove the `<supportedRuntime>` element.
If your app has an *App.config* file, remove the `<supportedRuntime>` element:
```xml
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
@@ -202,16 +293,18 @@ There are some things you should consider with the *App.config* file. The *App.c
## Add the compatibility package
If compilation fails and you receive errors similar to the following:
If your project file is loading correctly, but compilation fails for your project and you receive errors similar to the following:
- **The type or namespace \<some name> could not be found**
- **The name \<some name> does not exist in the current context**
You may need to add the [**Microsoft.Windows.Compatibility**](https://www.nuget.org/packages/Microsoft.Windows.Compatibility/) package to your app. This package adds ~21,000 .NET APIs from .NET Framework, such as the `System.Configuration.ConfigurationManager` class and APIs for interacting with the Windows Registry.
You may need to add the [`Microsoft.Windows.Compatibility`](https://www.nuget.org/packages/Microsoft.Windows.Compatibility/) package to your app. This package adds ~21,000 .NET APIs from .NET Framework, such as the `System.Configuration.ConfigurationManager` class and APIs for interacting with the Windows Registry. Add the `Microsoft.Windows.Compatibility` package.
Edit your project file and add the following `<ItemGroup>` element:
```xml
<ItemGroup>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="5.0.0-rc.2.20475.5" />
<PackageReference Include="Microsoft.Windows.Compatibility" Version="5.0.0" />
</ItemGroup>
```
@@ -55,10 +55,10 @@ namespace SDKSample {
myStackPanel->Children->Add(myTextBlock);
myStackPanel->Children->Add(myRectangle);
// Add the StackPanel as the lone Child of the Border
// Add the StackPanel as the lone child of the ScrollViewer
myScrollViewer->Content = myStackPanel;
// Add the Border as the Content of the Parent Window Object
// Add the ScrollViewer as the Content of the parent Window object
mainWindow->Content = myScrollViewer;
mainWindow->Show();
@@ -220,7 +220,7 @@
Value="True">
<Setter TargetName="ContentRow"
Property="Height"
Value="{Binding DesiredHeight, ElementName=Content}" />
Value="{Binding Height, ElementName=Content}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
@@ -49,10 +49,10 @@ namespace SDKSample
myStackPanel.Children.Add(myTextBlock);
myStackPanel.Children.Add(myRectangle);
// Add the StackPanel as the lone Child of the Border
// Add the StackPanel as the lone child of the ScrollViewer
myScrollViewer.Content = myStackPanel;
// Add the Border as the Content of the Parent Window Object
// Add the ScrollViewer as the Content of the parent Window object
mainWindow.Content = myScrollViewer;
mainWindow.Show ();
@@ -38,8 +38,10 @@ Namespace SDKSample
myStackPanel.Children.Add(myTextBlock)
myStackPanel.Children.Add(myRectangle)
'Add the StackPanel as the lone Child of the Border
'Add the StackPanel as the lone child of the ScrollViewer
myScrollViewer.Content = myStackPanel
'Add the ScrollViewer as the Content of the parent Window object
Me.Content = myScrollViewer
'</Snippet1>
End Sub