原來在 VS 內直接更名,在 git 版控上是先把原檔案進行刪除後,在建立一個檔案,Orz
直接下語法 git mv 原檔案名稱 新檔案名稱,就可以看見直接 rename 啦
檔案搬移一樣也是用 git mv 語法來做
git mv 檔案名稱 目標資料夾路徑,目標資料夾路徑假如有特殊符號,以自身情況來說,有資料夾是利用 . 符號命名 EX:[公司名.用途],需用 " 把路徑前後包起來就行,要不然會有下圖錯誤
namespace Xamarin.Forms
{
// Interface defining methods for two-way value conversion between types.
public interface IValueConverter
{
// Implement this method to convert value to targetType by using parameter and culture.
object Convert(object value, Type targetType, object parameter, CultureInfo culture);
// Implement this method to convert value back from targetType by using parameter and culture.
object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);
}
}
本篇是利用圖檔名稱透過 IValueConverter 轉換為 ImageResource 來 Binding Image View 來練習// 要使用 IValueConverter 要引用該 namespace
using Xamarin.Forms;
namespace DataBindingPractice
{
public class IValueConverterData
{
public string Photo { get; set; }
public IValueConverterData()
{
this.Photo = "maple.jpg";
}
}
public class ImageNameConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string ImageName = value.ToString();
return ImageSource.FromResource($"DataBindingPractice.{ImageName}");
}
// ConvertBack 只有 Binding Mode 設為 TwoWay 或 OneWayToSource 時才有使用
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Xaml 寫法<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:DataBindingPractice"
x:Class="DataBindingPractice.XF7_IValueConvert">
<ContentPage.Resources>
<ResourceDictionary>
<local:ImageNameConverter x:Key="cntImageName"></local:ImageNameConverter>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<Image
x:Name="img"
Source="{
Binding Photo ,
Converter={StaticResource Key=cntImageName}}"></Image>
</StackLayout>
</ContentPage.Content>
</ContentPage>
C# 寫法namespace DataBindingPractice
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class XF7_IValueConvert : ContentPage
{
public XF7_IValueConvert()
{
var Data = new IValueConverterData();
BindingContext = Data;
InitializeComponent();
img.SetBinding(Image.SourceProperty, nameof(Data.Photo), converter: new ImageNameConverter());
}
}
}
namespace DataBindingPractice
{
public class StringFormatData
{
public string EmpName { get; set; }
public decimal Salary { get; set; }
public DateTime HireDate { get; set; }
public StringFormatData()
{
EmpName = "Xamarin";
Salary = 49000;
HireDate = new DateTime(2008,3,3);
}
}
}
C# 內設定namespace DataBindingPractice
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class XF6_StringFormat : ContentPage
{
public XF6_StringFormat()
{
var Data = new StringFormatData();
BindingContext = Data;
InitializeComponent ();
lblEmpName.SetBinding(Label.TextProperty, nameof(Data.EmpName), stringFormat: "員工:{0}");
lblHireDate.SetBinding(Label.TextProperty, nameof(Data.HireDate), stringFormat: "到職日:{0:yyyy-MM-dd}");
lblSalary.SetBinding(Label.TextProperty, nameof(Data.Salary), stringFormat: "薪資:NT {0:N0}");
}
}
}
Xaml 內設定<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="DataBindingPractice.XF6_StringFormat">
<ContentPage.Content>
<StackLayout HorizontalOptions="Center" VerticalOptions="Center">
<Label x:Name="lblEmpName" Text="{Binding EmpName, StringFormat='員工:{0}'}" FontSize="Large"></Label>
<Label x:Name="lblHireDate" Text="{Binding HireDate, StringFormat='到職日:{0:yyyy-MM-dd}'}" FontSize="Large"></Label>
<Label x:Name="lblSalary" Text="{Binding Salary, StringFormat='薪資:NT {0:N0}'}" FontSize="Large"></Label>
</StackLayout>
</ContentPage.Content>
</ContentPage>
namespace DataBindingPractice
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Blog1_Simple : ContentPage
{
public Blog1_Simple()
{
InitializeComponent();
var Data = new MemberInfo();
// 設定 BindingContext,並在 Xaml 內設定 txtName DataBinding
this.BindingContext = Data;
// 利用 C# Code 建立 DataBinding
// 方法一
Binding bdEmail = new Binding()
{
Source = Data,
Path = "Email"
};
txtEmail.SetBinding(Entry.TextProperty, bdEmail);
// 方法二
txtEmail.BindingContext = Data;
txtEmail.SetBinding(Entry.TextProperty, nameof(Data.Email));
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="DataBindingPractice.Blog1_Simple">
<ContentPage.Content>
<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
<Entry x:Name="txtName" Text="{Binding Path=Name}"></Entry>
<Entry x:Name="txtEmail"></Entry>
</StackLayout>
</ContentPage.Content>
</ContentPage>
在 Xaml 內設定 BindingContext<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:DataBindingPractice"
x:Class="DataBindingPractice.Blog1_Simple">
<ContentPage.BindingContext>
<local:MemberInfo />
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
<Entry x:Name="txtName" Text="{Binding Path=Name}"></Entry>
<Entry x:Name="txtEmail" Text="{Binding Path=Email}"></Entry>
</StackLayout>
</ContentPage.Content>
</ContentPage>
擷取 Xamarin 官方文章說明Note: Typically, the runtime performance is better if BindableObject.BindingContext is set after all calls to BindableObject.SetBinding have been made.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:XFViewPractice"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="XFViewPractice.XF_DatePicker">
<ContentPage.Content>
<StackLayout
VerticalOptions="Center"
HorizontalOptions="Center">
<DatePicker
x:Name="DP"
VerticalOptions="Center"
HorizontalOptions="Center"
Date="{x:Static sys:DateTime.Today}"
Format="yyyy-MM-dd"
MinimumDate="2017-06-01"
MaximumDate="2017-08-31"
DateSelected="DP_DateSelected">
</DatePicker>
<Label
x:Name="lblResult"
Text="顯示點選結果">
</Label>
</StackLayout>
</ContentPage.Content>
</ContentPage>
C#namespace XFViewPractice
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class XF_DatePicker : ContentPage
{
public XF_DatePicker()
{
InitializeComponent();
}
private void DP_DateSelected(object sender, DateChangedEventArgs e)
{
lblResult.Text = string.Empty;
lblResult.Text += $"原資料:{e.OldDate.ToString("yyyy-MM-dd")}" + Environment.NewLine;
lblResult.Text += $"點選資料:{e.NewDate.ToString("yyyy-MM-dd")}";
}
}
}
重點紀錄:<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:XFViewPractice"
x:Class="XFViewPractice.XF_Picker">
<ContentPage.Content>
<StackLayout
HorizontalOptions="Center"
VerticalOptions="Center">
<Label
Text="Picker Demo"
FontSize="Large"
HorizontalOptions="Center" />
<Picker
x:Name="ColorPicker"
Title="選擇顏色"
VerticalOptions="CenterAndExpand"
SelectedIndex="1"
SelectedIndexChanged="ColorPicker_SelectedIndexChanged">
<Picker.Items>
<x:String>Aqua</x:String>
<x:String>Black</x:String>
<x:String>Blue</x:String>
<x:String>Fucshia</x:String>
<x:String>Gray</x:String>
<x:String>Green</x:String>
<x:String>Lime</x:String>
<x:String>Maroon</x:String>
<x:String>Navy</x:String>
<x:String>Olive</x:String>
<x:String>Purple</x:String>
<x:String>Red</x:String>
<x:String>Silver</x:String>
<x:String>Teal</x:String>
<x:String>White</x:String>
<x:String>Yellow</x:String>
</Picker.Items>
<Picker.SelectedIndex>4</Picker.SelectedIndex>
</Picker>
<BoxView
x:Name="ColorDisplay"
WidthRequest="150"
HeightRequest="150"
HorizontalOptions="Center"
VerticalOptions="Center"></BoxView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.reportViewer1.RefreshReport();
reportViewer1.PrintDialog();
}
}
}
上述語法會跳出下面這個錯誤namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.reportViewer1.RefreshReport();
}
private void reportViewer1_RenderingComplete(object sender, Microsoft.Reporting.WinForms.RenderingCompleteEventArgs e)
{
reportViewer1.PrintDialog();
}
}
}
File Name SyntaxManifest 檔案內的預設檔案內容
The name of an application manifest file is the name of the application's executable followed by .manifest.
For example, an application manifest that refers to Example.exe or Example.dll would use the following file name syntax. You can omit the <resource ID> field if resource ID is 1.
- example.exe.<resource ID>.manifest
- example.dll.<resource ID>.manifest
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC 資訊清單選項
如果要變更 Windows 使用者帳戶控制層級,請將
requestedExecutionLevel 節點以下列其中之一取代。
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
指定 requestedExecutionLevel 項目會停用檔案及登錄虛擬化。
如果您的應用程式需要針對回溯相容性進行這項虛擬化,請移除這個
項目。
-->
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- 這個應用程式測試所在及可配合使用的
Windows 版本清單。請取消註解適當的項目,Windows 將會
自動選取最相容的環境。-->
<!-- Windows Vista -->
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->
<!-- Windows 7 -->
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->
<!-- Windows 8 -->
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->
<!-- Windows 8.1 -->
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->
<!-- Windows 10 -->
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->
</application>
</compatibility>
<!-- 表示應用程式為 DPI 感知,Windows 不會在 DPI 變高時自動
調整。Windows Presentation Foundation (WPF) 應用程式會自動感知 DPI,因此不需要
選擇加入。選擇加入這項設定之以 .NET Framework 4.6 為目標的 Windows Form 應用程式也
應該在其 app.config 中將 'EnableWindowsFormsHighDpiAutoResizing' 設定為 'true'。-->
<!--
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
</windowsSettings>
</application>
-->
<!-- 啟用 Windows 通用控制項和對話方塊的佈景主題 (Windows XP 以後版本) -->
<!--
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
-->
</assembly>
新增 Manifest 後,在 Project 屬性內,預設會自動選取該 Manifest 檔案<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
true/pm
</dpiAware>
</windowsSettings>
</application>
另外 Manifest 檔案內還有提到 dotNet 4.6 以上,也要把 EnableWindowsFormsHighDpiAutoResizing 參數,加入 App.Config 檔案內<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />
</appSettings>
</configuration>
設定之後在 Windows 10 上,DPI 非 100% PC 上就可以輸出預期的報表,不過要特別注意,畢竟該設定會影響整個程式,要看看 WinForm Layout 會不會跑掉喔<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ImagePractice"
x:Class="ImagePractice.Internet">
<ContentPage.Content>
<StackLayout
HorizontalOptions="Center"
VerticalOptions="Center">
<Label Text="Web Demo" FontAttributes="Bold"></Label>
<Image
x:Name="imgWeb"
Aspect="AspectFit"
Source="https://www.xamarin.com/content/images/pages/forms/example-app.png"></Image>
</StackLayout>
</ContentPage.Content>
</ContentPage>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ImagePractice"
x:Class="ImagePractice.Internet">
<ContentPage.Content>
<StackLayout
HorizontalOptions="Center"
VerticalOptions="Center">
<Label Text="Web Demo" FontAttributes="Bold"></Label>
<Image
x:Name="imgWeb"
Aspect="AspectFit"
></Image>
</StackLayout>
</ContentPage.Content>
</ContentPage>
C# Codenamespace ImagePractice
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Internet : ContentPage
{
public Internet ()
{
InitializeComponent ();
// 方法一:直接給 url 會自動轉換為 uri
imgWeb.Source = "https://www.xamarin.com/content/images/pages/forms/example-app.png";
// 方法二
imgWeb.Source = ImageSource.FromUri(new Uri("https://www.xamarin.com/content/images/pages/forms/example-app.png"));
// 方法三
imgWeb.Source = new UriImageSource()
{
Uri = new Uri("https://www.xamarin.com/content/images/pages/forms/example-app.png"),
CachingEnabled = true , // 預設為 true
CacheValidity = new TimeSpan(0, 5, 0) // 預設快取是一天
};
}
}
}
結果:換張煙火圖來表示<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App1"
x:Class="App1.MainPage">
<StackLayout>
<Image
Source="Firework.jpg"
HeightRequest="200"
Aspect="AspectFit">
</Image>
</StackLayout>
</ContentPage>
Firework.jpg 位置InspectorDebugSession(0): StateChange: Start -> EntryPointBreakpointRegistered
InspectorDebugSession(0): Constructed
Android 應用程式正在偵錯。
Could not connect to the debugger.
InspectorDebugSession(0): HandleTargetEvent: TargetExited
InspectorDebugSession(0): Disposed
07-06 23:40:16.204 I/art ( 7613): Late-enabling -Xcheck:jni
07-06 23:40:16.244 W/monodroid( 7613): Creating public update directory: `/data/user/0/App2.Android/files/.__override__`
07-06 23:40:16.244 W/monodroid( 7613): Using override path: /data/user/0/App2.Android/files/.__override__
07-06 23:40:16.248 W/monodroid( 7613): Using override path: /storage/emulated/0/Android/data/App2.Android/files/.__override__
07-06 23:40:16.253 W/monodroid( 7613): Trying to load sgen from: /data/user/0/App2.Android/files/.__override__/libmonosgen-2.0.so
07-06 23:40:16.253 W/monodroid( 7613): Trying to load sgen from: /storage/emulated/0/Android/data/App2.Android/files/.__override__/libmonosgen-2.0.so
07-06 23:40:16.254 W/monodroid( 7613): Trying to load sgen from: /data/app/App2.Android-1/lib/x86/libmonosgen-2.0.so
07-06 23:40:16.259 W/monodroid( 7613): Trying to load sgen from: /data/user/0/App2.Android/files/.__override__/links/libmonosgen-2.0.so
07-06 23:40:16.261 W/monodroid-debug( 7613): Trying to initialize the debugger with options: --debugger-agent=transport=dt_socket,loglevel=0,address=127.0.0.1:29366,server=y,embedding=1
07-06 23:40:16.411 W/monodroid-debug( 7613): Accepted stdout connection: 10
07-06 23:40:17.189 D/Mono ( 7613): Image addref mscorlib[0xabb533c0] -> mscorlib.dll[0xadb2d800]: 2
07-06 23:40:17.191 D/Mono ( 7613): Prepared to set up assembly 'mscorlib' (mscorlib.dll)
07-06 23:40:17.192 D/Mono ( 7613): AOT: image 'mscorlib.dll.so' not found: dlopen failed: library "/data/app/App2.Android-1/lib/x86/libaot-mscorlib.dll.so" not found
07-06 23:40:17.192 D/Mono ( 7613): AOT: image '/usr/local/lib/mono/aot-cache/x86/mscorlib.dll.so' not found: dlopen failed: library "/data/app/App2.Android-1/lib/x86/libaot-mscorlib.dll.so" not found
07-06 23:40:17.192 D/Mono ( 7613): Config attempting to parse: 'mscorlib.dll.config'.
07-06 23:40:17.192 D/Mono ( 7613): Config attempting to parse: '/usr/local/etc/mono/assemblies/mscorlib/mscorlib.config'.
07-06 23:40:17.211 D/Mono ( 7613): Assembly mscorlib[0xabb533c0] added to domain RootDomain, ref_count=1