2017-10-16 6 views
1

J'utilise un exemple de Xamarin appelé "TableView pour un formulaire" pour tester une application et suis tombé sur une section obsolète, ImageSource = Device.OnPlatform qui est maintenant remplacé par une instruction switch . Aucun problème ici et beaucoup d'informations cependant, j'ai un problème particulier et ne peut pas voir le problème.Xamarin.Forms ImageSource = Device.OnPlatform obsolète

Le code que j'ajoute est actuellement commenté dans la source ci-dessous et compilera bien de cette façon, sans l'image bien sûr. Si je supprime la section commentée, j'obtiens une erreur sur la ligne 35, manquant}. Si je souligne l'accolade juste en dessous de la dernière coupure, il est conscient de sa correspondance, switch(). Si je souligne l'accolade ouverte juste en dessous, il pense qu'elle fait partie de Public SearchPage() en haut. Quelque chose dans le commutateur cause des problèmes mais je ne peux pas le voir.

J'espère que quelqu'un a rencontré cela et peut avoir une réponse. Faites-moi savoir si vous avez besoin de plus de détails.

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 
       } 
      }; 
     } 
    } 
} 

Répondre

0

Answered: Merci Scryptique

Je voulais poster le code exact que je utilise pour remplacer l'échantillon fourni par Xamarin pour la Galerie des formulaires - -> 'TableView pour un formulaire'.

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

Je ne pense pas que C# soutient switch déclarations dans initialiseurs d'objets comme ça. Le meilleur moyen de résoudre ce problème serait de refactoriser l'instruction switch en une méthode et de l'utiliser pour initialiser 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"); 
    } 
} 

et l'utiliser dans initialiseur:

new ImageCell 
{ 
    ImageSource = GetSource() 
} 
+0

C'est exactement ce dont il avait besoin. Je souhaite que Xamarin a été clair sur les changements dans leur application, mais heureux que cela l'a résolu. Merci –