首页 \ 问答 \ WP8 - 绑定ProgressBar可见性(WP8 - Bind ProgressBar visibility)

WP8 - 绑定ProgressBar可见性(WP8 - Bind ProgressBar visibility)

我有一个简单的代码,我检查了其他问题,但还没有。

我有一个应用程序,它从Web检索的xml文件中加载一些数据,然后在longlistselector中显示它。

我做到了,它的工作原理,现在我想添加一个不确定的进度条,它一直保持活动状态,直到我完成数据加载。

我将进度条包含在我的longlistselector之前的stackpanel中,并且我将其可见性限制在函数ProgressBarVisibility中(参见下面的代码)。

        <phone:PivotItem Header="Status">
            <StackPanel>
            <ProgressBar Value ="0" IsIndeterminate="True" Visibility="{Binding ProgressBarVisibility}"/>
            <phone:LongListSelector Margin="0,0,-12,0" ItemsSource="{Binding PivotOne}">
                <phone:LongListSelector.ItemTemplate>
                    <!-- lots of code here -->
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>
            </StackPanel>
        </phone:PivotItem>

在MainViewModel.cs中,这就是我写的东西。

    using System.Windows;


    public class MainViewModel : INotifyPropertyChanged
    {
    public MainViewModel()
    {
        this.PivotOne = new ObservableCollection<ItemViewModel>();
        this.PivotTwo = new ObservableCollection<ItemViewModel>();
        this.PivotThree = new ObservableCollection<ItemViewModel>();
    }

    /// <summary>
    /// A collection for ItemViewModel objects.
    /// </summary>
    public ObservableCollection<ItemViewModel> PivotOne { get; private set; }
    public ObservableCollection<ItemViewModel> PivotTwo { get; private set; }
    public ObservableCollection<ItemViewModel> PivotThree { get; private set; }

    private string _detailPageTitle = "Default";
    /// <summary>
    /// DetailPageTitle ritorna il titolo della pagina di dettaglio. Viene settato nella funzione che carica la pagina secondaria
    /// </summary>
    /// <returns></returns>
    public string DetailPageTitle
    {
        get
        {
            return _detailPageTitle;
        }
        set
        {
            if (value != _detailPageTitle)
            {
                _detailPageTitle = value;
                NotifyPropertyChanged("DetailPageTitle");
            }
        }
    }

    public bool IsDataLoaded
    {
        get;
        private set;
    }


    private Visibility _progressBarVisibility = Visibility.Collapsed;

    public Visibility ProgressBarVisibility
    {
        get
        {
            return _progressBarVisibility;
        }
        set
        {
            if (value != _progressBarVisibility)
            {
                _progressBarVisibility = value;
                NotifyPropertyChanged("ProgressBarVisibility");
            }
        }
    }


    private Visibility _progressBarVisibility = Visibility.Visible;

    public Visibility ProgressBarVisibility
    {
        get
        {
            return _progressBarVisibility;
        }
        set
        {
            if (value != _progressBarVisibility)
            {
                _progressBarVisibility = value;
                NotifyPropertyChanged("ProgressBarVisibility");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public void LoadData()
    {
        //progressbar is visible, data not loaded
        this.IsDataLoaded = false;
        ProgressBarVisibility = Visibility.Visible;

        // Load Static and dynamic data -- populate the different pivots
        LoadStaticData();
        LoadXMLFile();

        // data loaded, progressbar collapsed
        this.IsDataLoaded = true;
        ProgressBarVisibility = Visibility.Collapsed;
    }

所以我包括了system.windows库,并使用了可见性类。 无论如何,当加载完成时,我无法让进度条消失,它会继续运行。

有什么建议吗? 我在哪里做错了?

提前致谢!

解决方案:在应用程序激活时执行loaddata,因此此时内容甚至无法呈现。


I have a simple thing to code, i checked other questions but couldn't it yet.

I have an application which loads some data from an xml file retrieved from the web, and then displays it inside a longlistselector.

I did it, it works, now i would like to add an indeterminate progressbar which stays active until I finished the data loading.

I enclosed the progressbar in a stackpanel, before my longlistselector, and i bound its visibility to the function ProgressBarVisibility (see code below).

        <phone:PivotItem Header="Status">
            <StackPanel>
            <ProgressBar Value ="0" IsIndeterminate="True" Visibility="{Binding ProgressBarVisibility}"/>
            <phone:LongListSelector Margin="0,0,-12,0" ItemsSource="{Binding PivotOne}">
                <phone:LongListSelector.ItemTemplate>
                    <!-- lots of code here -->
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>
            </StackPanel>
        </phone:PivotItem>

In the MainViewModel.cs , that's how i wrote the thing.

    using System.Windows;


    public class MainViewModel : INotifyPropertyChanged
    {
    public MainViewModel()
    {
        this.PivotOne = new ObservableCollection<ItemViewModel>();
        this.PivotTwo = new ObservableCollection<ItemViewModel>();
        this.PivotThree = new ObservableCollection<ItemViewModel>();
    }

    /// <summary>
    /// A collection for ItemViewModel objects.
    /// </summary>
    public ObservableCollection<ItemViewModel> PivotOne { get; private set; }
    public ObservableCollection<ItemViewModel> PivotTwo { get; private set; }
    public ObservableCollection<ItemViewModel> PivotThree { get; private set; }

    private string _detailPageTitle = "Default";
    /// <summary>
    /// DetailPageTitle ritorna il titolo della pagina di dettaglio. Viene settato nella funzione che carica la pagina secondaria
    /// </summary>
    /// <returns></returns>
    public string DetailPageTitle
    {
        get
        {
            return _detailPageTitle;
        }
        set
        {
            if (value != _detailPageTitle)
            {
                _detailPageTitle = value;
                NotifyPropertyChanged("DetailPageTitle");
            }
        }
    }

    public bool IsDataLoaded
    {
        get;
        private set;
    }


    private Visibility _progressBarVisibility = Visibility.Collapsed;

    public Visibility ProgressBarVisibility
    {
        get
        {
            return _progressBarVisibility;
        }
        set
        {
            if (value != _progressBarVisibility)
            {
                _progressBarVisibility = value;
                NotifyPropertyChanged("ProgressBarVisibility");
            }
        }
    }


    private Visibility _progressBarVisibility = Visibility.Visible;

    public Visibility ProgressBarVisibility
    {
        get
        {
            return _progressBarVisibility;
        }
        set
        {
            if (value != _progressBarVisibility)
            {
                _progressBarVisibility = value;
                NotifyPropertyChanged("ProgressBarVisibility");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public void LoadData()
    {
        //progressbar is visible, data not loaded
        this.IsDataLoaded = false;
        ProgressBarVisibility = Visibility.Visible;

        // Load Static and dynamic data -- populate the different pivots
        LoadStaticData();
        LoadXMLFile();

        // data loaded, progressbar collapsed
        this.IsDataLoaded = true;
        ProgressBarVisibility = Visibility.Collapsed;
    }

So i included system.windows library, and used the visibility class. Anyway, i cannot get the progressbar to disappear when the loading is done, it keeps going.

Any suggestion? where am i doing it wrong?

Thanks in advance!

Solution: loaddata is executed on the app activation, so the content is not even rendered at that moment.


原文:https://stackoverflow.com/questions/18298263
更新时间:2023-12-31 22:12

最满意答案

<meta>元素仅在HTML5文档的<head>中有效( 元数据内容模型只能在<head> )。 您应该使用HTML5 jQuery插件来传递配置数据的方法是使用可以使用.data()从jQuery访问的data-*属性

所以你的标记可能是:

<div class="widget" data-foo="bar"><div>

而你的jQuery将是:

$('.widget').widget({...options...});

这将是一个函数,使用:

$.fn.widget = function (...) {
    return this.each(function () {
        ...code...
        options.foo = $(this).data('foo'); //gets the value 'bar'
        ...more code...
    });
};

回复OP的评论:

对于使用多少个data-*属性没有限制,如果仅仅是格式化,可以将它们分成多行。 白色空间以HTML压缩:

<div class="widget"
    data-foo="bar"
    data-fizz="buzz"
    data-lorem="ipsum"
    ...
    data-xyz="abc">
    ...contents...
</div>

如果您有充分的理由将数据与<div>分开,那么您可以使用type="hidden"<input>字段。当我有一个需要使用表单提交的交互式表单小部件时,我通常会使用它。


The <meta> element is only valid in the <head> of an HTML5 document (Metadata content model is only acceptable in the <head>). What you should be using for an HTML5 jQuery plugin to pass configuration data is to use data-* attributes which can be accessed from jQuery using .data().

So your markup could be:

<div class="widget" data-foo="bar"><div>

And your jQuery would be:

$('.widget').widget({...options...});

Which would be a function that uses:

$.fn.widget = function (...) {
    return this.each(function () {
        ...code...
        options.foo = $(this).data('foo'); //gets the value 'bar'
        ...more code...
    });
};

In reply to OPs comment:

There's no limit to how many data-* attributes you use, if it's simply for formatting, split them on multiple lines. White space is condensed in HTML:

<div class="widget"
    data-foo="bar"
    data-fizz="buzz"
    data-lorem="ipsum"
    ...
    data-xyz="abc">
    ...contents...
</div>

If you have a good reason to keep the data separate from the <div> you could use an <input> field with type="hidden" I typically use this when I have an interactive form widget that needs to be submitted with a form.

相关问答

更多
  • 你可以用正则表达式提取它。 类似于:/ /\]*\>(.*)\<\/body/m ^ /\]*\>(.*)\<\/body/m .*) /\]*\>(.*)\<\/body/m /\]*\>(.*)\<\/body/m - 应该返回元素中的所有内容。 $val = getData('myWebsite.html'); var reg = /\]*\>([^]*) ...
  • 你可以使用其中一个本机JS ... window.onload = function() { // your code here }; 还是jQuery ...... $(document).ready(function() { // your code here }); ...确保代码在文档加载完成之前不会运行。 这解释了window.onload和$(document).ready()之间的细微差别。 另一个选择是将代码包装在一个命名函数中并在某个地方调用它,但你仍然需要将它放在
  • 这当然是根据HTML4.01 无效的 。 META标签只允许在HEAD中 (就像TITLE一样),因此通过将其放入BODY,您实际上创建无效的标记。 从粗略测试,似乎有些浏览器(例如Firefox 3.5和Safari 4)在创建文档树时将这些元素实际放在HEAD中。 这不是非常令人惊讶:浏览器已知容忍并尝试解释各种破碎的标记。 无效标记很少是一个好主意 。 浏览器的非标准处理可能会导致各种难以针对的呈现(和行为)不一致。 而不是依靠浏览器猜测,最好遵循一个标准。 我不知道搜索引擎如何反应这样的标签汤,但我 ...
  • 元素仅在HTML5文档的中有效( 元数据内容模型只能在 )。 您应该使用HTML5 jQuery插件来传递配置数据的方法是使用可以使用.data()从jQuery访问的data-*属性 。 所以你的标记可能是:
    而你的jQuery将是: $('.widget').widget({...options...}); 这将是一个函数,使用: $.fn.widget = function (.. ...
  • 就我而言,它不是标准的,但受到所有常见浏览器的支持。 标准只定义了一个HTML标签,所以 document.documentElement 得到支持。 你可以检查: document.documentElement.children.length > 0 As far as I now, it's not standard, but supported by all common browsers. The standard only defines that there is a HTML tag, ...
  • 您首先需要提取