2016-11-11 144 views
-3

不能隱式地將類型「字符串」轉換爲「Windows.Security.Credentials.PasswordCredential」 經過大約4個小時的搜索後,我找不到這個錯誤。我在RPI 3上使用Windows 10 IOT Core。我的程序非常基本,但是我無法在代碼中轉換數據類型來解決錯誤。不能隱式地將類型「字符串」轉換爲「Windows.Security.Credentials.PasswordCredential」

using SDKTemplate; 
using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.IO; 
using Windows.Devices.WiFi; 
using Windows.Networking.Connectivity; 
using Windows.Security.Credentials; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Navigation; 

// The Blank Page item template is documented at   http://go.microsoft.com/fwlink/?LinkId=234238 

namespace WiFiConnect 
{ 
/// <summary> 
/// An empty page that can be used on its own or navigated to within a   Frame. 
/// </summary> 
public sealed partial class WiFiConnect_Scenario : Page 
{ 
    MainPage rootPage; 
    private WiFiAdapter firstAdapter; 
    public ObservableCollection<WiFiNetworkDisplay> ResultCollection 
    { 
     get; 
     private set; 
    } 

    public WiFiConnect_Scenario() 
    { 
     this.InitializeComponent(); 
    } 

    protected override async void OnNavigatedTo(NavigationEventArgs e) 
    { 
     ResultCollection = new ObservableCollection<WiFiNetworkDisplay>(); 
     rootPage = MainPage.Current; 

     // RequestAccessAsync must have been called at least once by the app  before using the API 
     // Calling it multiple times is fine but not necessary 
     // RequestAccessAsync must be called from the UI thread 
     var access = await WiFiAdapter.RequestAccessAsync(); 
     if (access != WiFiAccessStatus.Allowed) 
     { 
      rootPage.NotifyUser("Access denied", NotifyType.ErrorMessage); 
     } 
     else 
     { 
      DataContext = this; 

      var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDevice  Selector()); 
      if (result.Count >= 1) 
      { 
       firstAdapter = await WiFiAdapter.FromIdAsync(result[0].Id); 

       var button = new Button(); 
       button.Content = string.Format("Scan Available Wifi  Networks"); 
       button.Click += Button_Click; 
       Buttons.Children.Add(button); 
      } 
      else 
      { 
       rootPage.NotifyUser("No WiFi Adapters detected on this  machine.", NotifyType.ErrorMessage); 
      } 
     } 
    } 

    private async void Button_Click(object sender, RoutedEventArgs e) 
    { 
     await firstAdapter.ScanAsync(); 
     ConnectionBar.Visibility = Visibility.Collapsed; 
     DisplayNetworkReport(firstAdapter.NetworkReport); 
    } 

    private void DisplayNetworkReport(WiFiNetworkReport report) 
    { 
     rootPage.NotifyUser(string.Format("Network Report Timestamp: {0}",  report.Timestamp), NotifyType.StatusMessage); 

     ResultCollection.Clear(); 

     foreach (var network in report.AvailableNetworks) 
     { 
      ResultCollection.Add(new WiFiNetworkDisplay(network, firstAdapter)); 
     } 
    } 

    private void ResultsListView_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     var selectedNetwork = ResultsListView.SelectedItem as WiFiNetworkDisplay; 
     if (selectedNetwork == null) 
     { 
      return; 
     } 

     // Show the connection bar 
     ConnectionBar.Visibility = Visibility.Visible; 

     // Only show the password box if needed 
     if (selectedNetwork.AvailableNetwork.SecuritySettings.NetworkAuthenticationType == NetworkAuthenticationType.Open80211 && 
       selectedNetwork.AvailableNetwork.SecuritySettings.NetworkEncryptionType == NetworkEncryptionType.None) 
     { 
      NetworkKeyInfo.Visibility = Visibility.Collapsed; 
     } 
     else 
     { 
      NetworkKeyInfo.Visibility = Visibility.Visible; 
     } 
    } 

    private async void ConnectButton_Click(object sender, RoutedEventArgs e) 
    { 
     var selectedNetwork = ResultsListView.SelectedItem as WiFiNetworkDisplay; 
     if (selectedNetwork == null || firstAdapter == null) 
     { 
      rootPage.NotifyUser("Network not selcted", NotifyType.ErrorMessage); 
      return; 
     } 
     WiFiReconnectionKind reconnectionKind = WiFiReconnectionKind.Manual; 
     if (IsAutomaticReconnection.IsChecked.HasValue && IsAutomaticReconnection.IsChecked == true) 
     { 
      reconnectionKind = WiFiReconnectionKind.Automatic; 
     } 

     WiFiConnectionResult result; 
     if (selectedNetwork.AvailableNetwork.SecuritySettings.NetworkAuthenticationType == Windows.Networking.Connectivity.NetworkAuthenticationType.Open80211 && 
       selectedNetwork.AvailableNetwork.SecuritySettings.NetworkEncryptionType == NetworkEncryptionType.None) 
     { 
      result = await firstAdapter.ConnectAsync(selectedNetwork.AvailableNetwork, reconnectionKind); 
     } 
     else 
     { 

      FileStream file = new FileStream("final-wordlist.txt", FileMode.Open, FileAccess.Read); 
      StreamReader sr = new StreamReader(file); 
      sr.ReadLine(); 
      var textLines = File.ReadAllLines("final-wordlist.txt"); 
      foreach (var line in textLines) 
      { 
       string[] dataArray = line.Split(' '); 




       foreach (var item in dataArray) 
       { 
        PasswordCredential credential = line; 
        //string credential = line.ToString(); 



      result = await firstAdapter.ConnectAsync(selectedNetwork.AvailableNetwork, reconnectionKind, credential); 
       } 


      } 
      // Only the password potion of the credential need to be supplied 

     } 

     if (result.ConnectionStatus == WiFiConnectionStatus.Success) 
     { 
      rootPage.NotifyUser(string.Format("Successfully connected to {0}.", selectedNetwork.Ssid), NotifyType.StatusMessage); 

      // refresh the webpage 
      webViewGrid.Visibility = Visibility.Visible; 
      toggleBrowserButton.Content = "Hide Browser Control"; 
      refreshBrowserButton.Visibility = Visibility.Visible; 

     } 
     else 
     { 
      rootPage.NotifyUser(string.Format("Could not connect to {0}. Error: {1}", selectedNetwork.Ssid, result.ConnectionStatus), NotifyType.ErrorMessage); 
     } 

     // Since a connection attempt was made, update the connectivity level displayed for each 
     foreach (var network in ResultCollection) 
     { 
      network.UpdateConnectivityLevel(); 
     } 
    } 

    private void Browser_Toggle_Click(object sender, RoutedEventArgs e) 
    { 
     if (webViewGrid.Visibility == Visibility.Visible) 
     { 
      webViewGrid.Visibility = Visibility.Collapsed; 
      refreshBrowserButton.Visibility = Visibility.Collapsed; 
      toggleBrowserButton.Content = "Show Browser Control"; 
     } 
     else 
     { 
      webViewGrid.Visibility = Visibility.Visible; 
      refreshBrowserButton.Visibility = Visibility.Visible; 
      toggleBrowserButton.Content = "Hide Browser Control"; 
     } 
    } 
    private void Browser_Refresh(object sender, RoutedEventArgs e) 
    { 
     webView.Refresh(); 
    } 
} 
} 
+1

它給你一個行號嗎?請參閱以下內容,因爲我懷疑此代碼的一小部分與此問題無關:http://stackoverflow.com/help/mcve – EJoshuaS

+0

它給了我行150 –

+0

代碼示例中對應的代碼行? – EJoshuaS

回答

0

試試這個:

PasswordCredential credential = null; 

if (!string.IsNullOrEmpty(line)) 
{ 
    credential = new PasswordCredential() 
    { 
     Password = line 
    }; 
} 
0

這爲我工作。問題在於它需要多個變種來填充它。

var credential = new PasswordCredential("Module", "Username", "Password"); 
相關問題