微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xamarin表单使用Web服务

如何解决Xamarin表单使用Web服务

我是移动应用程序的新手,现在正在使用xamarin表单开发应用程序。我用django(sqlite3 db)开发了一个网站,现在我正尝试使用其中的数据并在listvew的移动应用程序中显示。任何想法如何实现它。我已经尝试过了,但是没有用。我应该使用rest api吗?

forloop.counter

型号:

{% for post in posts %}
<article class="media content-section">
  <div class="media-body">
    <h2><a id="post_title_{{ forloop.counter }}" class="article-title" href="{% url 'post-detail' post.slug %}">{{ post.title }}</a></h2>
    <div class="article-Metadata">
      <a class="mr-2" href="{% url 'blog-profile' name=post.author %}">{{ post.author }}</a>
      <div class="float-right">
        <small class="text-muted">Category</small>
        <small class="text-muted">{{ post.date_posted }}</small>
      </div>
      <div style="float:right;">
        <img style="height:19px; width:18px;" src="{% static " blog/viewicon.png " %}">
        <p style="float: right; display: inline !important;" id="ViewCount__{{ forloop.counter }}">

        </p>
        </img>
      </div>
    </div>
    <p class="article-content">{{ post.content|truncatechars:200|safe }}</p>
  </div>
</article>
{% endfor %}

我在Django中的数据库

public class Landingviewmodel : INotifyPropertyChanged
{
    private List<dishes> _menuList { set; get; }
    public List<dishes> MenuList 
    {
        get
        {
            return _menuList;
        }
        set
        {
            if(value != _menuList)
            {
                _menuList = value;
                NotifyPropertyChanged();
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
    }
    

    public Landingviewmodel()
    {
        GetDataAsync();
    }

    private async void GetDataAsync()
    {
        HttpClient client = new HttpClient();

        var response = await client.GetAsync("https://mysite.ru/project/");
        if (response.IsSuccessstatusCode)
        {
            var content = await response.Content.ReadAsstringAsync();
            var menu = JsonConvert.DeserializeObject<List<dishes>>(content);
            MenuList = new List<dishes>(menu);
        }
    }

解决方法

我解决了问题

    PostgreSQL数据库中的
  1. 允许远程连接:在postgresql.conf中,用listen_addresses ='*'代替了行listen_addresses ='localhost'

  2. 允许在端口5432上进行tcp \ ip连接

  3. 在我的Web API中建立的连接宽度db

    APPADMINS or APPUSERS

我的应用中的代码:

 NpgsqlConnection connection;

 public string _name;
 public string _description;
 public string _image;

 private readonly MenuContext _context;

 public MenuController(MenuContext context)
 {
     _context = context;

     string connectionString = "Server=server; Port=5432; User Id=user; 
     Password=password; Database=database";

     try
     {
         connection = new NpgsqlConnection(connectionString);
         connection.Open();
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }

     NpgsqlCommand command = connection.CreateCommand();
     command.CommandText = "SELECT * FROM table";
     try
     {
         NpgsqlDataReader reader = command.ExecuteReader();

         while (reader.Read())
         {
             _name = reader[1].ToString();
             _image = reader[2].ToString();
             _description = reader[3].ToString();

             _context.Menus.Add(new Menu { name = _name,description = 
             _description,image = "https://mysite.ru/media/" + _image });
             _context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
 }

 // GET: api/Menu
 [HttpGet]
 public async Task<ActionResult<IEnumerable<Menu>>> GetMenus()
 {
     return await _context.Menus.ToListAsync();
 }

然后将其显示在列表视图中

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。