[参考までにWindows10,Windows7 だけでBuildできるサンプルを載せておきます]
一つのフォルダーに次のファイルをメモ帳などでつくります。
App.xaml
コード:
<Application x:Class="test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:test"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
App.xaml.cs
コード:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace test
{
/// <summary>
/// App.xaml の相互作用ロジック
/// </summary>
public partial class App : Application
{
}
}
MainWindow.xaml
コード:
<Window x:Class="test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:test"
Title="MainWindow" Height="480" Width="640">
<Canvas>
<Rectangle
x:Name="rect"
Fill="Blue" Height="100" Stroke="Black" Width="100"
Canvas.Left="10" Canvas.Top="10"/>
<Button Content="right" Canvas.Left="432" Canvas.Top="10" Width="75" Height="40" Click="button1_Click"/>
<Button Content="down" Canvas.Left="432" Canvas.Top="55" Width="75" Height="40" Click="button2_Click"/>
<Button Content="set" Canvas.Left="432" Canvas.Top="100" Width="75" Height="40" Click="button3_Click"/>
</Canvas>
</Window>
MainWindow.xaml.cs
コード:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace test
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// 右へ移動
private void button1_Click(object sender, RoutedEventArgs e)
{
double x = Canvas.GetLeft(this.rect);
Canvas.SetLeft(this.rect, x + 20);
}
// 下へ移動
private void button2_Click(object sender, RoutedEventArgs e)
{
double y = Canvas.GetTop(this.rect);
Canvas.SetTop(this.rect, y + 20);
}
// 初期値へ戻す
private void button3_Click(object sender, RoutedEventArgs e)
{
Canvas.SetLeft(this.rect, 10);
Canvas.SetTop(this.rect, 10);
}
}
}