我从JSON文件创建了ListView
。现在我想从被点击的ViewCell
传递数据,这样我就可以继续我的流程了。
以下是这个函数listViewJson_ItemTapped
最终应该在每个项目中执行的操作:
$"{id}-{curDateTime}.jpg"
现在我不知道如何从点击的ViewCell
传递数据。此外,我坚持第二张强制照片。到目前为止,我的代码可以拍摄1张照片,但上传会抛出一个未处理的异常(现在从IIS端看到这个问题)。
这是来自ListView
:
<ListView x:Name="listViewJson" Grid.Column="0" BackgroundColor="White" ItemTapped="listViewJson_ItemTapped">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid HorizontalOptions="Center" VerticalOptions="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Grid.Column="0" Text="{Binding id}" FontAttributes="Bold" />
<Label Grid.Column="1" Text="{Binding fzg_kz}" FontAttributes="Bold" />
<Label Grid.Column="2" Text="{Binding timestamp}" FontAttributes="Bold" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这是listViewJson_ItemTapped
中的C#代码隐藏:
private async void listViewJson_ItemTapped(object sender, EventArgs e)
{
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await DisplayAlert("No Camera", ":( No camera available.", "OK");
return;
}
DateTime dt = DateTime.Now.AddHours(2);
var format = "MM/dd/yyyy_hh:mm:ss";
String curDateTime = dt.ToString(format);
var _mediaFile = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Directory = "Sample",
Name = $"{curDateTime}.jpg"
});
if (_mediaFile == null)
return;
var image = new Image();
image.Source = ImageSource.FromStream(() =>
{
return _mediaFile.GetStream();
});
var content = new MultipartFormDataContent();
content.Add(new StreamContent(_mediaFile.GetStream()), "\"file\"", $"\"{_mediaFile.Path}\"");
var httpClient = new HttpClient();
var uploadServiceBaseAddress = "http://xxx.xxx.xxx.xxx/files";
var httpResponseMessage = await httpClient.PostAsync(uploadServiceBaseAddress, content);
};
}
这个问题确实让我头疼,因为没有这个我就无法继续工作。 我是编程新手,所以我们非常感谢任何建议和参考。
先谢谢,保罗
答案 0 :(得分:0)
因此,如果我理解正确,那么您应该获得唯一的文件名:
((ObjectType)(e.Item)).Id.ToString() + "-" + DateTime.Now.ToString();
不要忘记用你用来填充列表的对象交换ObjectType。
对于相机两次我建议只做一个简单的循环,如果你愿意的话,你确实可以使用一个简单的进度条。
到目前为止,该未处理的例外有任何进展吗?我想做的就是找到这样的错误,就是在输出调试中写json,将json复制到Postman中,然后发送它。比你得到一个比Unhandled更好的错误
我希望这很清楚,gl!
为id编辑一些额外的例子。
例如,你得到了以下JSON:
{
"name":"John",
"age":30
}
现在,如果它在列表中并且您单击了,则需要转换它。 我们创建了Class Called Person并给出了这2个字段(名称和年龄) 公共阶层人 { 私人字符串名称; private int age;
public Person() {
}
public int Age
{
get { return age; }
set { age = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
}
现在你可以使用我先给你的代码,只需用Object替换ObjectType。
((Person)(e.Item)).Id.ToString() + "-" + DateTime.Now.ToString();
我如何上传我的图片(请注意这是一个脏版本,这意味着它仍然需要清理一些我为测试原因而放置的脏代码,它还有调试窗口中的json仍然存在,所以使用它如果它不起作用,我使用的客户端类似Httpclient,我给了它一个基地址。如果有什么不清楚,请不要犹豫要求
public static bool InsertAllFotos(tTodo ttodo, List<Fotos> fotos, List<string> blob)
{
//var test2 = blob[0].blob_data.Bytes;
List<tSyndFoto> tFotos = new List<tSyndFoto>();
foreach(var foto in fotos)
{
tSyndFoto tFoto = new tSyndFoto();
tFoto.id_synd_dossier = ttodo.id_gebouw;
tFoto.omschrijving = foto.Omschrijving;
tFoto.id_todo = ttodo.id_todo;
tFotos.Add(tFoto);
}
JObject json = new JObject();
json.Add("fotos", JsonConvert.SerializeObject(tFotos));
//tBlob tmpBlob = new tBlob();
//tmpBlob.blob_data = new Binary();
//tmpBlob.blob_data.Bytes = new byte[0];
// tmpBlob.blob_data.Bytes = blob[0].blob_data.Bytes;
//var lijst = new List<tBlob>();
//lijst.Add(tmpBlob);
json.Add("blobs", JsonConvert.SerializeObject(blob));
//json.Add("blobs", null);
//string test = json.ToString();
//var jon2 = JsonConvert.DeserializeObject(blob[0].blob_data.Bytes.ToString());
var test = json.ToString();
test = test.Replace("\"[", "[");
test = test.Replace("]\"", "]");
// test = test.Replace("\\n", "");
test = test.Replace("\\", "");
Debug.WriteLine(test);
var content = new StringContent(test, Encoding.UTF8, "application/json");
var resp = client.PostAsync("InsertFotos", content);
if (resp.Result.IsSuccessStatusCode)
{
var repStr = resp.Result.Content.ReadAsStringAsync();
JObject jo = JObject.Parse(repStr.Result.ToString());
return jo.SelectToken("InsertFotosResult", false).ToObject<bool>();
}
return false;
}
这是我从中获得的JSON(blobs是我的图片,我删除了一些数据,因为它太大了:
{
"fotos": [{"id_synd_foto":"00000000-0000-0000-0000-000000000000","id_blob":null,"id_synd_dossier":"xxx","web_image":null,"uploaded":null,"omschrijving":null,"datum":null,"id_todo":"xx","id_synd_eigenaarsnet_melding":null,"id_eigenaar":null,"id_synd_kavel":null,"bijlage_melding":null}],
"blobs": ["/9j/4QDqRXhpZgAASUkqAAgAAAAJAAiSAwABAAAAAAAAABIBAwABAAAAAAAAABABAgAqAAAAegAAAAeSAwABAAAA///////wCGmP2av+jhvBH/AIVNn/8AHaP+GmP2av8Ao4bwR/4VNn/8dr+xD++Dt6K4j/hpj9mr/o4bwR/4VNn/APHaP+GmP2av+jhvBH/hU2f/AMdoA7eiuI/4aY/Zq/6OG8Ef+FTZ/wDx2j/hpj9mr/o4bwR/4VNn/wDHaAPzv/4KM/8AJ6XjH/uHf+m+2rxKvoj9tL4Z/Er4x/tPeJfiR8IPh5rfinw9qP2P+z9d8OaVNfWV15dnDFJ5c8Ksj7Z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