【发布时间】:2019-11-16 19:51:58
【问题描述】:
我正在 Xamarin.Forms 中做一个天气应用项目。在成功将用户添加到数据库并在“登录”时验证现有用户之后,现在我正在努力在 SQLite 数据库中添加一个城镇(带有 3 个变量),然后在内容页面上的 ListView 中显示信息。 问题是,当我单击“天气”按钮时,ListView 中没有显示任何内容,并且触发了第二个捕获 - Temp = "Unable to get Weather";
让我们从我的城镇模型开始:
public class Town
{
[PrimaryKey]
public int ID { get; set; }
public string TownName { get; set; }
public string Temp { get; set; }
public string searchTime { get; set; }
public Town() { }
}
创建这个简单的类后,我正在实现一个控制器类来处理城镇数据:
SQLiteConnection database;
public SearchHistoryDataController()
{
database = DependencyService.Get<ISQLite>().GetConnection();
database.CreateTable<Town>();
}
IEnumerable<Town> orderItemCollection;
public IEnumerable<Town> OrderItemCollection
{
get
{
if (orderItemCollection == null)
orderItemCollection = GetTowns();
return orderItemCollection;
}
}
public IEnumerable<Town> GetTowns()
{
// Changing the database table items as ObservableCollection
var table = (from i in database.Table<Town>() select i);
ObservableCollection<Town> TownList = new ObservableCollection<Town>();
foreach (var town in table)
{
TownList.Add(new Town()
{
ID = town.ID,
TownName = town.TownName,
Temp = town.Temp,
searchTime = town.searchTime
});
}
return TownList;
}
public Town GetTown(int id)
{
return database.Table<Town>().FirstOrDefault(t => t.ID == id);
}
public void DeleteTown(int id)
{
database.Delete<Town>(id);
}
public string AddTown(Town town)
{
var data = database.Table<Town>();
var d1 = data.Where(x => x.TownName == town.TownName && x.searchTime == town.searchTime).FirstOrDefault();
if (d1 == null)
{
database.Insert(town);
return "Successfully Added";
}
else
{
return "Invalid Town id Exist";
}
}
从这里开始,最后两件事是: 创建一个城镇,插入一些信息,将城镇存储在数据库中,每当单击“显示天气”按钮时,这都是在命令中完成的。有代码:
//Get weather information for the Weather view
Temp = $"{weatherRoot?.MainWeather?.Temperature ?? 0}°C";
Condition = $"{weatherRoot?.Weather?[0]?.Description ?? string.Empty}";
Name = $"{weatherRoot.Name}, {weatherRoot.System.Country}";
Humidity = $"{weatherRoot.MainWeather.Humidity}%";
Pressure = $"{weatherRoot.MainWeather.Pressure} hpa";
Clouds = $"{weatherRoot.Clouds.CloudinessPercent}%";
Wind = $"{weatherRoot.Wind.Speed} m/s";
town.TownName = $"{weatherRoot.Name}";
town.Temp = Temp;
town.searchTime = DateTime.Parse(weatherRoot.Date).ToLocalTime().ToString("g");;
//history database push
try
{
SearchHistoryDataController searchHistoryDataController = new SearchHistoryDataController();
var returnvalue = searchHistoryDataController.AddTown(town);
if (returnvalue == "Sucessfully Added")
{
Console.WriteLine("Success");
}
else
{
Console.WriteLine("Fail");
}
}
catch (Exception es)
{
Debug.WriteLine(es.Message);
}
await TextToSpeech.SpeakAsync(Temp + " " + Condition);
IsBusy = false;
}
catch (Exception ex)
{
Temp = "Unable to get Weather";
Debug.WriteLine(ex.Message);
}
finally
{
IsBusy = false;
}
}
最终的 - XAML 文件的结构如下:
<Grid>
<Image HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFill" Source="gradientBack.png"/>
<StackLayout Padding="10" Spacing="10">
<StackLayout Orientation="Horizontal" Spacing="20" HorizontalOptions="Center" VerticalOptions="Start" Margin="5,20">
<Label Text="Search History" FontSize="20" FontAttributes="Bold" VerticalOptions="Center" TextColor="White" HorizontalOptions="Center"/>
</StackLayout>
<ListView ItemsSource="{Binding OrderItemCollection}"
HasUnevenRows="True"
CachingStrategy="RecycleElement"
IsPullToRefreshEnabled="True"
RefreshCommand="{Binding GetWeatherCommand}"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
RowHeight="66"
x:Name="ListViewTide">
<ListView.SeparatorColor>
<OnPlatform x:TypeArguments="Color" iOS="Transparent"/>
</ListView.SeparatorColor>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame BackgroundColor="White" BorderColor="#F0F0F0" Padding="5" Margin="0,0,0,5" HasShadow="False">
<Grid HeightRequest="50" HorizontalOptions="FillAndExpand" VerticalOptions="Start">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Text="{Binding DisplayTide}" TextColor="#800080" FontSize="15"
FontAttributes="Bold"
Style="{DynamicResource ListItemTextStyle}"/>
<StackLayout Grid.Column="2" Orientation="Horizontal" Margin="20,0" HorizontalOptions="End" VerticalOptions="Center">
<Label Text="{Binding DisplayTime}" TextColor="Black" FontSize="15" FontAttributes="None" VerticalOptions="Center"/>
</StackLayout>
</Grid>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</Grid>
这个想法是在点击“天气”按钮时自动更新历史列表。历史将在文本字段中存储城镇名称、准确时间和温度。 该列表不会用于其他任何内容,它是一个选项卡。
非常感谢!
【问题讨论】:
-
导致“无法获取天气”消息的实际异常是什么?
-
这是关于天气信息的。它工作正常,如果我删除有关城镇数据库的部分,则不会触发此异常。
-
杰森,你能不能冷静一点。没有异常消息,因为不存在现有错误。如果有异常消息可以帮助您解决问题,我一定会分享。我已经解决了这个问题。 BR
标签: c# android sql visual-studio xamarin