Skip to content

Commit 72008e3

Browse files
drag behavior sample
1 parent bf74046 commit 72008e3

17 files changed

+317
-0
lines changed

5_More/WinUI/BehaviorSample/App.xaml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Application
2+
x:Class="BehaviorSample.App"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:local="using:BehaviorSample">
6+
<Application.Resources>
7+
<ResourceDictionary>
8+
<ResourceDictionary.MergedDictionaries>
9+
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
10+
<!-- Other merged dictionaries here -->
11+
</ResourceDictionary.MergedDictionaries>
12+
<!-- Other app resources here -->
13+
</ResourceDictionary>
14+
</Application.Resources>
15+
</Application>
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Microsoft.UI.Xaml;
2+
3+
// To learn more about WinUI, the WinUI project structure,
4+
// and more about our project templates, see: http://aka.ms/winui-project-info.
5+
6+
namespace BehaviorSample;
7+
8+
/// <summary>
9+
/// Provides application-specific behavior to supplement the default Application class.
10+
/// </summary>
11+
public partial class App : Application
12+
{
13+
/// <summary>
14+
/// Initializes the singleton application object. This is the first line of authored code
15+
/// executed, and as such is the logical equivalent of main() or WinMain().
16+
/// </summary>
17+
public App() => InitializeComponent();
18+
19+
/// <summary>
20+
/// Invoked when the application is launched normally by the end user. Other entry points
21+
/// will be used such as when the application is launched to open a specific file.
22+
/// </summary>
23+
/// <param name="args">Details about the launch request and process.</param>
24+
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
25+
{
26+
m_window = new MainWindow();
27+
m_window.Activate();
28+
}
29+
30+
private Window? m_window;
31+
}
Loading
Loading
Loading
Loading
Loading
456 Bytes
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>WinExe</OutputType>
4+
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
5+
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
6+
<RootNamespace>BehaviorSample</RootNamespace>
7+
<ApplicationManifest>app.manifest</ApplicationManifest>
8+
<Platforms>x86;x64;arm64</Platforms>
9+
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
10+
<PublishProfile>win10-$(Platform).pubxml</PublishProfile>
11+
<UseWinUI>true</UseWinUI>
12+
<ImplicitUsings>enable</ImplicitUsings>
13+
<Nullable>enable</Nullable>
14+
<EnableMsixTooling>true</EnableMsixTooling>
15+
</PropertyGroup>
16+
17+
<ItemGroup>
18+
<Content Include="Assets\SplashScreen.scale-200.png" />
19+
<Content Include="Assets\LockScreenLogo.scale-200.png" />
20+
<Content Include="Assets\Square150x150Logo.scale-200.png" />
21+
<Content Include="Assets\Square44x44Logo.scale-200.png" />
22+
<Content Include="Assets\Square44x44Logo.targetsize-24_altform-unplated.png" />
23+
<Content Include="Assets\StoreLogo.png" />
24+
<Content Include="Assets\Wide310x150Logo.scale-200.png" />
25+
</ItemGroup>
26+
27+
<ItemGroup>
28+
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.1.0" />
29+
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22000.194" />
30+
<PackageReference Include="Microsoft.Xaml.Behaviors.WinUI.Managed" Version="2.0.8" />
31+
<Manifest Include="$(ApplicationManifest)" />
32+
</ItemGroup>
33+
34+
<!--
35+
Defining the "Msix" ProjectCapability here allows the Single-project MSIX Packaging
36+
Tools extension to be activated for this project even if the Windows App SDK Nuget
37+
package has not yet been restored.
38+
-->
39+
<ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
40+
<ProjectCapability Include="Msix" />
41+
</ItemGroup>
42+
43+
<!--
44+
Defining the "HasPackageAndPublishMenuAddedByProject" property here allows the Solution
45+
Explorer "Package and Publish" context menu entry to be enabled for this project even if
46+
the Windows App SDK Nuget package has not yet been restored.
47+
-->
48+
<PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
49+
<HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
50+
</PropertyGroup>
51+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.3.32611.2
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BehaviorSample", "BehaviorSample.csproj", "{527CBCDF-16B6-4DE4-865D-8436AED31C00}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|arm64 = Debug|arm64
11+
Debug|x64 = Debug|x64
12+
Debug|x86 = Debug|x86
13+
Release|arm64 = Release|arm64
14+
Release|x64 = Release|x64
15+
Release|x86 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{527CBCDF-16B6-4DE4-865D-8436AED31C00}.Debug|arm64.ActiveCfg = Debug|arm64
19+
{527CBCDF-16B6-4DE4-865D-8436AED31C00}.Debug|arm64.Build.0 = Debug|arm64
20+
{527CBCDF-16B6-4DE4-865D-8436AED31C00}.Debug|arm64.Deploy.0 = Debug|arm64
21+
{527CBCDF-16B6-4DE4-865D-8436AED31C00}.Debug|x64.ActiveCfg = Debug|x64
22+
{527CBCDF-16B6-4DE4-865D-8436AED31C00}.Debug|x64.Build.0 = Debug|x64
23+
{527CBCDF-16B6-4DE4-865D-8436AED31C00}.Debug|x64.Deploy.0 = Debug|x64
24+
{527CBCDF-16B6-4DE4-865D-8436AED31C00}.Debug|x86.ActiveCfg = Debug|x86
25+
{527CBCDF-16B6-4DE4-865D-8436AED31C00}.Debug|x86.Build.0 = Debug|x86
26+
{527CBCDF-16B6-4DE4-865D-8436AED31C00}.Debug|x86.Deploy.0 = Debug|x86
27+
{527CBCDF-16B6-4DE4-865D-8436AED31C00}.Release|arm64.ActiveCfg = Release|arm64
28+
{527CBCDF-16B6-4DE4-865D-8436AED31C00}.Release|arm64.Build.0 = Release|arm64
29+
{527CBCDF-16B6-4DE4-865D-8436AED31C00}.Release|arm64.Deploy.0 = Release|arm64
30+
{527CBCDF-16B6-4DE4-865D-8436AED31C00}.Release|x64.ActiveCfg = Release|x64
31+
{527CBCDF-16B6-4DE4-865D-8436AED31C00}.Release|x64.Build.0 = Release|x64
32+
{527CBCDF-16B6-4DE4-865D-8436AED31C00}.Release|x64.Deploy.0 = Release|x64
33+
{527CBCDF-16B6-4DE4-865D-8436AED31C00}.Release|x86.ActiveCfg = Release|x86
34+
{527CBCDF-16B6-4DE4-865D-8436AED31C00}.Release|x86.Build.0 = Release|x86
35+
{527CBCDF-16B6-4DE4-865D-8436AED31C00}.Release|x86.Deploy.0 = Release|x86
36+
EndGlobalSection
37+
GlobalSection(SolutionProperties) = preSolution
38+
HideSolutionNode = FALSE
39+
EndGlobalSection
40+
GlobalSection(ExtensibilityGlobals) = postSolution
41+
SolutionGuid = {235A967B-A579-44CC-A198-9131463E2DB9}
42+
EndGlobalSection
43+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using Microsoft.UI.Xaml;
2+
using Microsoft.UI.Xaml.Media;
3+
using Microsoft.Xaml.Interactivity;
4+
5+
using Windows.Foundation;
6+
7+
namespace BehaviorSample;
8+
9+
public class DragBehavior : Behavior<FrameworkElement>
10+
{
11+
private TranslateTransform _transform = new();
12+
private FrameworkElement? _parent;
13+
private Point _prevPoint;
14+
private long _pointerId;
15+
16+
protected override void OnAttached()
17+
{
18+
base.OnAttached();
19+
20+
AssociatedObject.RenderTransform = _transform;
21+
22+
AssociatedObject.PointerPressed += (sender, e) =>
23+
{
24+
if (AssociatedObject.Parent is FrameworkElement parent)
25+
{
26+
_parent = parent;
27+
28+
_prevPoint = e.GetCurrentPoint(_parent).Position;
29+
_parent.PointerMoved += (sender, e) =>
30+
{
31+
if (e.Pointer.PointerId != _pointerId) return;
32+
33+
var pos = e.GetCurrentPoint(_parent).Position;
34+
_transform.X += pos.X - _prevPoint.X;
35+
_transform.Y += pos.Y - _prevPoint.Y;
36+
_prevPoint = pos;
37+
};
38+
_pointerId = e.Pointer.PointerId;
39+
}
40+
};
41+
42+
AssociatedObject.PointerReleased += (sender, e) =>
43+
{
44+
if (e.Pointer.PointerId != _pointerId) return;
45+
_pointerId = -1;
46+
};
47+
}
48+
49+
protected override void OnDetaching()
50+
{
51+
_parent = null;
52+
_pointerId = -1;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Window
2+
x:Class="BehaviorSample.MainWindow"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:i="using:Microsoft.Xaml.Interactivity"
6+
xmlns:local="using:BehaviorSample"
7+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
8+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9+
mc:Ignorable="d">
10+
11+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
12+
<Border Background="LightBlue" Padding="12">
13+
<i:Interaction.Behaviors>
14+
<local:DragBehavior />
15+
</i:Interaction.Behaviors>
16+
<!--<Button x:Name="myButton" Click="myButton_Click">Drag me</Button>-->
17+
<TextBlock Text="move me" />
18+
</Border>
19+
20+
</StackPanel>
21+
</Window>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.UI.Xaml;
2+
using Microsoft.UI.Xaml.Controls;
3+
using Microsoft.UI.Xaml.Controls.Primitives;
4+
using Microsoft.UI.Xaml.Data;
5+
using Microsoft.UI.Xaml.Input;
6+
using Microsoft.UI.Xaml.Media;
7+
using Microsoft.UI.Xaml.Navigation;
8+
9+
using System;
10+
using System.Collections.Generic;
11+
using System.IO;
12+
using System.Linq;
13+
using System.Runtime.InteropServices.WindowsRuntime;
14+
15+
using Windows.Foundation;
16+
using Windows.Foundation.Collections;
17+
18+
// To learn more about WinUI, the WinUI project structure,
19+
// and more about our project templates, see: http://aka.ms/winui-project-info.
20+
21+
namespace BehaviorSample;
22+
23+
/// <summary>
24+
/// An empty window that can be used on its own or navigated to within a Frame.
25+
/// </summary>
26+
public sealed partial class MainWindow : Window
27+
{
28+
public MainWindow() => InitializeComponent();
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<Package
4+
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
5+
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
6+
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
7+
IgnorableNamespaces="uap rescap">
8+
9+
<Identity
10+
Name="0e81efdb-d289-4f71-bf3f-cf9104827311"
11+
Publisher="CN=ChristianNagel"
12+
Version="1.0.0.0" />
13+
14+
<Properties>
15+
<DisplayName>BehaviorSample</DisplayName>
16+
<PublisherDisplayName>ChristianNagel</PublisherDisplayName>
17+
<Logo>Assets\StoreLogo.png</Logo>
18+
</Properties>
19+
20+
<Dependencies>
21+
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.17763.0" MaxVersionTested="10.0.19041.0" />
22+
<TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.17763.0" MaxVersionTested="10.0.19041.0" />
23+
</Dependencies>
24+
25+
<Resources>
26+
<Resource Language="x-generate"/>
27+
</Resources>
28+
29+
<Applications>
30+
<Application Id="App"
31+
Executable="$targetnametoken$.exe"
32+
EntryPoint="$targetentrypoint$">
33+
<uap:VisualElements
34+
DisplayName="BehaviorSample"
35+
Description="BehaviorSample"
36+
BackgroundColor="transparent"
37+
Square150x150Logo="Assets\Square150x150Logo.png"
38+
Square44x44Logo="Assets\Square44x44Logo.png">
39+
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" />
40+
<uap:SplashScreen Image="Assets\SplashScreen.png" />
41+
</uap:VisualElements>
42+
</Application>
43+
</Applications>
44+
45+
<Capabilities>
46+
<rescap:Capability Name="runFullTrust" />
47+
</Capabilities>
48+
</Package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"profiles": {
3+
"BehaviorSample (Package)": {
4+
"commandName": "MsixPackage"
5+
},
6+
"BehaviorSample (Unpackaged)": {
7+
"commandName": "Project"
8+
}
9+
}
10+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
3+
<assemblyIdentity version="1.0.0.0" name="BehaviorSample.app"/>
4+
5+
<application xmlns="urn:schemas-microsoft-com:asm.v3">
6+
<windowsSettings>
7+
<!-- The combination of below two tags have the following effect:
8+
1) Per-Monitor for >= Windows 10 Anniversary Update
9+
2) System < Windows 10 Anniversary Update
10+
-->
11+
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/PM</dpiAware>
12+
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
13+
</windowsSettings>
14+
</application>
15+
</assembly>

0 commit comments

Comments
 (0)