2017-10-16 60 views
1

我使用Xamarin中稱爲「TableView for a form」的樣本來測試應用程序,並且遇到了一個過時的部分,ImageSource = Device.OnPlatform,現在用switch語句。這裏沒有問題,但有很多信息,我有一個奇怪的問題,不能看到問題。Xamarin.Forms ImageSource = Device.OnPlatform過時

我添加的代碼目前已經在下面的代碼中註釋掉了,並且可以用這種方式編譯,當然沒有圖片。如果我刪除評論部分,則會在第35行中發現錯誤,缺少}。 如果我突出顯示最後一個突破下面的花括號,它會發現它的匹配switch()。如果我突出顯示下面的開放大括號,它認爲它是公共SearchPage()的一部分。 交換機中的某些東西導致問題,但我無法看到它。

我希望有人遇到此問題並可能有答案。讓我知道你是否需要更多細節。

using System; 
using System.Collections.Generic; 
using System.Text; 

using Xamarin.Forms; 
//using static System.Net.Mime.MediaTypeNames; 

namespace MiddleMeeter 
{ 
    class SearchPage : ContentPage 
    { 
     public SearchPage() 
     { 
      Label header = new Label 
      { 
       Text = "TableView for a form", 
       FontSize = 30, 
       FontAttributes = FontAttributes.Bold, 
       HorizontalOptions = LayoutOptions.Center 
      }; 

      TableView tableView = new TableView 
      { 
       Intent = TableIntent.Form, 
       Root = new TableRoot("TableView Title") 
       { 
        new TableSection("Table Section") 
        { 
         new TextCell 
         { 
          Text = "Text Cell", 
          Detail = "With Detail Text", 
         }, 
         new ImageCell 
         { 
          /********************************************************************************************** 
          switch (Device.RuntimePlatform) 
          { 
           case Device.iOS: 
           ImageSource.FromUri(new Uri("http://xamarin.com/images/index/ide-xamarin-studio.png")); 
            break; 
           case Device.Android: 
            ImageSource.FromFile("waterfront.jpg"); 
            break; 
           case Device.WinPhone: 
            ImageSource.FromFile("Images/waterfront.jpg"); 
            break; 
           default: 
            ImageSource.FromFile("Images/waterfront.jpg"); 
            break; 
          }, 
          */////////////////////////////////////////////////////////////////////////////////////////////// 

          Text = "Image Cell", 
          Detail = "With Detail Text", 
         }, 
         new SwitchCell 
         { 
          Text = "Switch Cell" 
         }, 
         new EntryCell 
         { 
          Label = "Entry Cell", 
          Placeholder = "Type text here" 
         }, 
         new ViewCell 
         { 
          View = new Label 
          { 
           Text = "A View Cell can be anything you want!" 
          } 
         } 
        }, 
       } 
      }; 

      // Build the page. 
      this.Content = new StackLayout 
      { 
       Children = 
       { 
        header, 
        tableView 
       } 
      }; 
     } 
    } 
} 

回答

0

回答:謝謝Scryptique

我想後,我使用,以取代由Xamarin的窗體庫提供的樣本確切的代碼 - - >'表格的表格'。

using System; 
using System.Collections.Generic; 
using System.Text; 

using Xamarin.Forms; 
//using static System.Net.Mime.MediaTypeNames; 

namespace MiddleMeeter 
{ 
    class SearchPage : ContentPage 
    { 
     public SearchPage() 
     { 
      Label header = new Label 
      { 
       Text = "TableView for a form", 
       FontSize = 30, 
       FontAttributes = FontAttributes.Bold, 
       HorizontalOptions = LayoutOptions.Center 
      }; 

      TableView tableView = new TableView 
      { 
       Intent = TableIntent.Form, 
       Root = new TableRoot("TableView Title") 
       { 
        new TableSection("Table Section") 
        { 
         new TextCell 
         { 
          Text = "Text Cell", 
          Detail = "With Detail Text", 
         }, 
         new ImageCell 
         { 
          // This is the call to method getSource() 
          ImageSource = getSource(), 
          Text = "Image Cell", 
          Detail = "With Detail Text", 
         }, 
         new SwitchCell 
         { 
          Text = "Switch Cell" 
         }, 
         new EntryCell 
         { 
          Label = "Entry Cell", 
          Placeholder = "Type text here" 
         }, 
         new ViewCell 
         { 
          View = new Label 
          { 
           Text = "A View Cell can be anything you want!" 
          } 
         } 
        }, 
       } 
      }; 

      // Build the page. 
      this.Content = new StackLayout 
      { 
       Children = 
       { 
        header, 
        tableView, 
       } 
      }; 


     } 

     // Method to get the format to retreive the image for Platform specific detaisl 
     private ImageSource getSource() 
     { 
      switch (Device.RuntimePlatform) 
      { 
       case Device.iOS: 
        return ImageSource.FromUri(new Uri("https://www.xamarin.com/content/images/pages/branding/assets/xamagon.png")); 
       case Device.Android: 
        return ImageSource.FromFile("Icon.png"); 
       case Device.WinPhone: 
        return ImageSource.FromFile("Images/waterfront.jpg"); 
       default: 
        return ImageSource.FromFile("Images/waterfront.jpg"); 
      } 
     } 
    } 
} 
2

我不認爲c#支持switch像這樣的對象初始值設定語句。解決此問題的最佳方法是將switch語句重構爲一個方法,並使用它來初始化ImageCell

ImageSource GetSource() 
{ 
    switch (Device.RuntimePlatform) 
    { 
     case Device.iOS: 
      return ImageSource.FromUri(new Uri("http://xamarin.com/images/index/ide-xamarin-studio.png")); 
     case Device.Android: 
      return ImageSource.FromFile("waterfront.jpg"); 
     case Device.WinPhone: 
      return ImageSource.FromFile("Images/waterfront.jpg"); 
     default: 
      return ImageSource.FromFile("Images/waterfront.jpg"); 
    } 
} 

而且在初始化使用它:

new ImageCell 
{ 
    ImageSource = GetSource() 
} 
+0

這正是它所需要的。我希望Xamarin清楚他們的應用程序的變化,但很高興解決了它。謝謝 –