6.在Xamarin.Forms中使用XAML代码创建页面

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_22033759/article/details/49340335

xaml代码的方式也是和WPF的差不多,比如写一个label

<Label Text="Hello from XAML!"  
      IsVisible="True"     
      XAlign="Center"     
      TextColor="Blue"        
      FontSize="Large"        
      FontAttributes="Bold,Italic" /> 

但是目前还是有个比较明显的缺点就是和代码一样,没有设计器,不能直接看到界面的预览。

下面就开始写个吧。
首先先创建一个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="App15.Page1">   
               <ContentPage.Content>      
                 <StackLayout>           
                   <StackLayout.Children>         
                     <Frame OutlineColor="Accent">             
                       <Frame.Content>                       
                         <StackLayout Orientation="Horizontal">   
                           <StackLayout.Children>                 
                             <BoxView Color="Red" />              
                           <Label Text="Red"                    
                                  VerticalOptions="Center" />     
                           </StackLayout.Children> 
                     </StackLayout>                  
                     </Frame.Content>             
                     </Frame>  
                <Frame OutlineColor="Accent">            
                  <Frame.Content>                     
                    <StackLayout Orientation="Horizontal">   
                      <StackLayout.Children>                   
                        <BoxView Color="Green" />              
                     <Label Text="Green"                           
                            VerticalOptions="Center" />            
                     </StackLayout.Children>                      
                   </StackLayout>              
                 </Frame.Content>        
               </Frame>  

                <Frame OutlineColor="Accent">       
                  <Frame.Content>                     
                    <StackLayout Orientation="Horizontal">      
                      <StackLayout.Children>                      
                        <BoxView Color="Blue" />                   
              <Label Text="Blue"                                 
                     VerticalOptions="Center" />                
                      </StackLayout.Children>                    
                </StackLayout>                 
              </Frame.Content>          
            </Frame>        
          </StackLayout.Children>
        </StackLayout>  
      </ContentPage.Content>
 </ContentPage> 

把App.cs文件里写一下就可以了:

 public class App : Application
    {
        public App()
        {
            MainPage = new Page1();
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }

效果图:
这里写图片描述

展开阅读全文

加载xaml页面出错

04-16

刚安装完2010的professional的beta2版,新建Silverlight项目时,提示安装web developer和Silverlight sdk。于是上官网下载了这两个东西。安装之后可以新建Silverlight项目,但是打开xaml页面时,设计视图却出现错误。错误信息如下:rnSystem.ArgumentNullExceptionrnValue cannot be null.rnParameter name: parentContextrn at Microsoft.Windows.Design.Metadata.ReflectionMetadataContext..ctor(IMetadataContext parentContext)rn at MS.Internal.Host.ProjectMetadataContext..ctor(IMetadataContext platformMetadata, AssemblyReferenceProvider referenceProvider)rn at MS.Internal.Host.ProjectMetadataContext.FromReferences(AssemblyReferenceProvider referenceProvider)rn at MS.Internal.Designer.VSDesigner.GetMetadataForDesignerContext(DesignerContext designerContext)rn at MS.Internal.Host.PersistenceSubsystem.Load()rn at MS.Internal.Host.Designer.Load()rn at MS.Internal.Designer.VSDesigner.Load()rn at MS.Internal.Designer.VSIsolatedDesigner.VSIsolatedView.Load()rn at MS.Internal.Designer.VSIsolatedDesigner.VSIsolatedDesignerFactory.Load(IsolatedView view)rn at MS.Internal.Host.Isolation.IsolatedDesigner.BootstrapProxy.LoadDesigner(IsolatedDesignerFactory factory, IsolatedView view)rn at MS.Internal.Host.Isolation.IsolatedDesigner.BootstrapProxy.LoadDesigner(IsolatedDesignerFactory factory, IsolatedView view)rn at MS.Internal.Host.Isolation.IsolatedDesigner.Load()rn at MS.Internal.Designer.DesignerPane.LoadDesignerView()rnrn忘知情者告知 论坛

没有更多推荐了,返回首页