[英]How to Request Location permission before showing map set up in Xaml. Xamarin.Forms
[英]How to check if a link request is valid before it sends it? (Xamarin.Forms)
我正在做一個簡單的測試項目,如果您在一個條目中輸入任何股票代碼,它會生成一個 WebClient 和返回該股票價格字符串的鏈接。
我知道我需要從中獲取數據的確切 URL,如果用戶輸入現有的股票代碼,它工作得很好。 但是,如果用戶制作股票代碼,我會遇到運行時錯誤,因為程序試圖從不存在的 web 頁面中提取數據。
在運行應用程序時,它直接進入中斷模式並返回錯誤System.NullReferenceException: 'Object reference not set to an instance of an object.'
.
如何在發出請求之前檢查 URL 是否有效,或者至少能夠在將應用程序置於中斷模式之前捕獲錯誤?
這是 c# 代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Stock_WatchList
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class sybmolSelector : ContentPage
{
public string price { get; set; }
public sybmolSelector()
{
InitializeComponent();
BindingContext = this;
}
private void Entry_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
var nameValue = entry.Text.ToString();
var link = "https://sandbox.iexapis.com/stable/stock/" + nameValue + "/price?token=Tsk_57bebc1d051340dbaad8656ab0027e90";
var client1 = new WebClient();
string b = client1.DownloadString(link);
price = "$" + b;
Symbol.Text = nameValue.ToString();
Price.Text = price;
}
}
}
和 Xamarin 代碼:
<?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="Stock_WatchList.sybmolSelector"
BackgroundColor="Black"
NavigationPage.IconColor="HotPink">
<ContentPage.Content>
<StackLayout>
<Label TextColor="HotPink" BackgroundColor="Black" Text="------------------- Enter A Symbol Below -------------------" FontSize="19" LineBreakMode="TailTruncation"></Label>
<Entry PlaceholderColor="HotPink" PropertyChanged="Entry_PropertyChanged" FontSize="19" x:Name="entry" TextColor="HotPink" BackgroundColor="#111111"></Entry>
<Label x:Name="Symbol" Margin="5" Text="" HorizontalOptions="Start" TextColor="HotPink" FontSize="40"/>
<Label x:Name="Price" Margin="5,0,5,5" Text="" HorizontalOptions="Start" TextColor="HotPink" FontSize="50"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
如果 Entry.Text 為 null 或為空,請勿撥打電話。 當您的表單被加載時,PropertyChanged 將被調用。 那時 Text 仍然是 null。 您的 markdown 中當然沒有默認設置。
private void Entry_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
// guard for null
if (entry == null) return;
// no need to call the api if we don't have a value
if (String.IsNullOrEmpty(entry.Text)) return;
var link = "https://sandbox.iexapis.com/stable/stock/" + entry.Text + "/price?token=sometoken";
using(var client1 = new WebClient()) // WebClient does implement (useless) IDisposable
{
price = "$" + client1.DownloadString(link);
}
// maybe these are null as well, who knows
if (Symbol != null) Symbol.Text = entry.Text;
if (Price != null) Price.Text = price;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.